Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ArrayList change object position

Posted on 2007-11-25
2
Medium Priority
?
1,459 Views
Last Modified: 2013-11-23
Hi all
I want to change the position between 2 objects in an arraylist.
i.e. if my arrayList contains objects as follows:
myObj1
myObj2
myObj3
myObj4
I want to change myObj2 - myObj3:
myObj1
myObj3
myObj2
myObj4

Any ideas?
0
Comment
Question by:thanassis
2 Comments
 
LVL 10

Accepted Solution

by:
effx earned 500 total points
ID: 20345800
myObjTmp = myObj2
myObj2 = myObj3
myObj3 = myObjTmp
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20346913
Use this,
 
public void swap(List l, int x, int y) {
    Object t = l.get(x);
  l.set(x, l.get(y));
    l.set(y, t);
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question