This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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();
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).
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