4

There are two ArrayList e.g.list1 = {1,2,3} and list2 = {7,6,4,2} How to can swap these two list. So the result will be list1 = {7,6,4,2} and list2 = {1,2,3} Can I implement like this:

public void swapList(ArrayList<Integer> list1, ArrayList<Integer> list2){
    ArrayList<Integer> tmpList = list1;
    list1 = list2;
    list2 = tmpList;
}
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
Renjun Zou
  • 101
  • 2
  • 8
  • +1 You asked below the follow-up question "Is there any less memory cost way to solve this problem? Since I may have much element in my list." Consider asking that as a separate StackOverflow question. Show how you're using this method and ask that question. I think it's a really good one. – Erick Robertson Feb 29 '12 at 00:06

5 Answers5

14

No you can't implement it like that. Same with arrays. Pass-reference-by-value problem, like the others explained already.

If you want the lists to swap their content, then you have to clear and copy:

public static void swapList(List<Integer> list1, List<Integer> list2){
    List<Integer> tmpList = new ArrayList<Integer>(list1);
    list1.clear();
    list1.addAll(list2);
    list2.clear();
    list2.addAll(tmpList);
}

Some additional thoughts:

List<Integer> list1 = getList1Magic();
List<Integer> list2 = getList2Magic();

if (isSwapReferences()) {
  // this does not affect the actual lists
  List<Integer> temp = list2;
  list2 = list1;
  list1 = temp;
} else if (isSwapListContent()) {
  // this modifies the lists
  swapList(list1, list2);  // method from above
}

The swap strategy depends on your requirements. First block has a local effect, second block a global one.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • This is precise solution, customization can also be made for null values of the arguments (list1 and list2) – Goran Nastov Feb 28 '12 at 12:23
  • Keep in mind that this is much slower. If you can, you should swap just the references. – Piotr Praszmo Feb 28 '12 at 12:24
  • It makes my head hurt to think about all the memory and CPU being wasted here. – Erick Robertson Feb 28 '12 at 12:38
  • 2
    This is *Part 1: How To Do It*. *Part 2: How To Avoid It* is sold separately ;) – Andreas Dolk Feb 28 '12 at 12:42
  • Is there any less memory cost way to solve this problem? Since I may have much element in my list. – Renjun Zou Feb 28 '12 at 13:24
  • 1
    Sure: don't use a method to swap. Do it, where you have defined your variables (local variables or members) and swap the references there. If you want to swap in a method, than it gets expensive. *And*: the swapped content effects *all* other variables, that hold references to the lists. They see the swapped content too. So it depends on your requirement. – Andreas Dolk Feb 28 '12 at 13:35
4

The problem with your solution is that Java is a pass-by-value language. So any change to list1 and list2 variables will not have an effect outside the method, but if this is not in a method, then it's ok.

Two more things:

  1. You probably meant list2 = tmpList;, not list2 = list1;.
  2. You cannot use generic with primitives, should be List<Integer>.
Jianxin Gao
  • 2,717
  • 2
  • 19
  • 32
MByD
  • 135,866
  • 28
  • 264
  • 277
2

You can't do it in a function because of Java is pass by value only. And you can't create a List of int, you can create a List of Integer.

You can try this to verify that it does not work:

public static void main(String... args) throws Exception {
    List<Integer> list1 = Arrays.asList(1, 2, 3);
    List<Integer> list2 = Arrays.asList(4, 5, 6);
    System.out.println(list1); //prints 1, 2, 3
    swapList(list1, list2);
    System.out.println(list1); //prints 1, 2, 3
}

public static void swapList(List<Integer> list1, List<Integer> list2){
    List<Integer> tmpList = list1;
    list1 = list2;
    list2 = tmpList;
}
Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
0

Minor correction in your approach:

    ArrayList<Integer> tmpList = list1;
    list1 = list2;
    list2 = tmpList;

But you can not do this in a method the way you are doing.

Kuldeep Jain
  • 8,409
  • 8
  • 48
  • 73
-3

You can use the swap method from Collections:

Collections.swap(Integer, list1, list2);
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
John
  • 1
  • [this is now how swap works](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html) – oers Oct 01 '12 at 10:02