Solved

ArrayList change object position

Posted on 2007-11-25
2
1,447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
effx earned 125 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

622 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