File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Swapping objects between two arraylists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Swapping objects between two arraylists" Watch "Swapping objects between two arraylists" New topic

Swapping objects between two arraylists

Naveen Ramanathan

Joined: Apr 20, 2009
Posts: 8

I was wondering if there is a way to swap objects between two arraylists? I have two ArrayLists of Players with a skill number (random number between 1-10) and am having a hard time figuring out how to swap the Player with the minimum skill from either team(team1/team2) with the Player with the maximum skill from either team (team1/team2).

The logic is as below:

SkillDifferenceCompare skillDifferenceCompare = new SkillDifferenceCompare();

swapListMin.add(Collections.min(team1, skillDifferenceCompare));
swapListMax.add(Collections.max(team1, skillDifferenceCompare));

swapListMin.add(Collections.min(team2, skillDifferenceCompare));
swapListMax.add(Collections.max(team2, skillDifferenceCompare));

swapListMin.remove(Collections.max(swapListMin, skillDifferenceCompare));
swapListMax.remove(Collections.min(swapListMax, skillDifferenceCompare));

System.out.println("Before Swapping: ");

What I am doing is finding the Player with the min skill in either team1/team2 using a comparator(on skilldifference instance variable of type Integer of Player), storing them in a swapListMin arraylist then finding the Player with the max skill in either team1/team2 using the same comparator and storing them in swapListMin.
At the point, swapListMin has 2 Players with min skills from team1 and team 2. swapListMax has 2 Players with max skills from team1 and team2. Then I remove the min skill Player from swapListMax and max skill Player from swapListMin. This leaves me with just 1 element in swapListMin (who is basically the Player with the lowest skill in either of the teams put together) and the opposite in swapListMax.

What i want to do now is to swap these players in swapListMin and swapListMax and put them back in their opposite teams (swap them basically). I was able to swap the Players from swapListMin and swapListMax using a tempList. But I am not able to figure out how to put them back in the team arraylist (team1 or team2).

tempSwapList = swapListMin;
swapListMin = swapListMax;
swapListMax = tempSwapList;

The Player object has attributes: playerTeam (either 1 or 2), playerSorted( false by default, to be set to true once swapping is done), skillDifference(random number between 1 to 10).

I would really appreciate it if someone could guide me on how to go about performing the swap.

Thanks in advance !

Ankit Garg

Joined: Aug 03, 2008
Posts: 9280

Hi Naveen, welcome to javaranch.

Well I couldn't understand the complete logic that you are trying to explain here, but swaping objects in ArrayList should not be a tough task. ArrayList is ordered and also provides index based access to elements. So it should be as simple as this

[Edit: added welcome message]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Naveen Ramanathan

Joined: Apr 20, 2009
Posts: 8
Naveen Ramanathan

Joined: Apr 20, 2009
Posts: 8
Thanks Ankit.. I got it to work finally...
My apologies for the long delay in reply..

I agree. Here's the link:
subject: Swapping objects between two arraylists
Similar Threads
<identifier> expected problem - Vectors.
CMR Relations one-to-many
I hate NullPointerExceptions!
calling a method w/out an object
Where have I gone wrong?