0

When I tried to add inner list to outer list with following elements and again cleared and added inner list with different elements. The output of program is repeating the elements of second list

import java.util.*;
public class HelloWorld{

     public static void main(String []args){

        ArrayList<ArrayList <Integer>> outer = new ArrayList<ArrayList <Integer>>();

        ArrayList<Integer> inner = new ArrayList<Integer>();

        inner.add(1);
        inner.add(2);
        inner.add(3);
        inner.add(4);

        outer.add(inner);

        inner.clear();

        inner.add(5);
        inner.add(6);
        inner.add(7);
        inner.add(8);

        outer.add(inner);
        System.out.println(outer);

     }
}

Output - [[5,6,7,8],[5,6,78]]

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • 1
    Because you're working with the reference of this list. You need to create a new instance rather than clearing the list. – maio290 Feb 27 '20 at 09:56
  • this is normal, since you actually change the original List. Your outer List only contains references to the original list. The reference may not change, but the content of that list does. – Stultuske Feb 27 '20 at 09:56
  • You have cleared the inner list, but as you are dealing with reference the new values will be updated at every occurrence. – Ashutosh Feb 27 '20 at 09:58
  • @maio290 can you please explain with example how should I correct it. – irkhaladkar Feb 27 '20 at 09:58
  • @irkhaladkar you know how to create a new instance of an ArrayList? that is what you should do, instead of clearing the first one – Stultuske Feb 27 '20 at 09:58
  • @Stultuske how should I edit this so that I can get output [[1,2,3,4],[5,6,7,8]] – irkhaladkar Feb 27 '20 at 10:00
  • 1
    @irkhaladkar `outer.add(inner)` --> `outer.add(new ArrayList<>(inner))` – Ivar Feb 27 '20 at 10:01
  • 1
    @irkhaladkar please start by learning the basics. creating a new instance is not advanced material. – Stultuske Feb 27 '20 at 10:06
  • What's the output you are expecting. Can you make an edit and add it? – Hassam Abdelillah Feb 27 '20 at 10:19

3 Answers3

3
import java.util.*;
public class HelloWorld {

    public static void main(String[] args){
        ArrayList<ArrayList<Integer>> outer = new ArrayList<ArrayList<Integer>>();

        ArrayList<Integer> inner = new ArrayList<Integer>();

        inner.add(1);
        inner.add(2);
        inner.add(3);
        inner.add(4);

        outer.add(inner);

        inner = new ArrayList<Integer>();

        inner.add(5);
        inner.add(6);
        inner.add(7);
        inner.add(8);

        outer.add(inner);
        System.out.println(outer);
    }
}

Output - [[1,2,3,4],[5,6,7,8]]

ysakhno
  • 834
  • 1
  • 7
  • 17
0

In java, by default every object is copied by reference until you mention it therefore when you add inner(ArrayList) to outer(ArrayList) a reference to the inner is added as a row of the 2d array list outer and if you change anything on the inner it will be reflected in the outer list as well as because it is accessing by reference no copy is created. The inner variable is just holding a reference of some set of values so you have to create another reference to add a different set of values in outer ArrayList

Sunil Lulla
  • 797
  • 10
  • 18
0

ArrayList stores references to objects so outer list is having [5,6,78]. You need to create separate Lists. You may also want to simplify list creation with Arrays.asList(),

ArrayList<ArrayList <Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>(Arrays.asList(1,2,3,4)));
outer.add(new ArrayList<>(Arrays.asList(5,6,7,8)));
Vikas
  • 6,868
  • 4
  • 27
  • 41