2

i have 2 arraylist

 ArrayList<List<Integer>> main = new ArrayList<>(); 
 ArrayList<Integer> sub=new ArrayList<>();
 sub.add(1);
 sub.add(2);
 main.add(sub);
 sub.clear();
 sub.add(5);
 sub.add(6);
 sub.add(7);
 main.add(sub);

now i expect main to be

what i expect main-->[[1,2],[5,6,7]] ;
but really main-->[[567],[567]];

i think sub array share reference ..so how can i make

main as [[1,2],[5,6,7]

i can't create sub1 ,sub2,...because actually i do this inside huge loop

asked Oct 2, 2014 at 4:22
3
  • 1
    Create a new ArrayList instead of clearing the one. Commented Oct 2, 2014 at 4:23
  • why do u do sub.clear(); Commented Oct 2, 2014 at 4:23
  • you can create clone for the same check out my answer Commented Oct 2, 2014 at 4:32

5 Answers 5

6

You were modifying the list and adding it one more time, so that's why [567] appeared twice. I suggest you change code as following:

ArrayList<List<Integer>> main = new ArrayList<>(); 
ArrayList<Integer> sub = new ArrayList<>();
sub.add(1);
sub.add(2);
main.add(sub);
sub = new ArrayList<>();
sub.add(5);
sub.add(6);
sub.add(7);
main.add(sub);
answered Oct 2, 2014 at 4:25
Sign up to request clarification or add additional context in comments.

Comments

2

You are referring to same instance

sub.add(1);
sub.add(2);
main.add(sub);
sub.clear(); // you are clearing same instance you added to `main` list

you need to create new instance

You need

 ArrayList<Integer> sub1 = new ArrayList<>();
 sub1.add(1);
 sub1.add(2);
 main.add(sub1);
 ArrayList<Integer> sub2 = new ArrayList<>();
 sub2.add(10);
 sub2.add(20);
 main.add(sub2);
answered Oct 2, 2014 at 4:23

Comments

2

Adding to an ArrayList using the .add function, adds the reference of the same memory address onto the ArrayList.

So in your solution, a reference pointing to what sub was pointing to was added in the ArrayList. This reference internally pointed to the same location.

Now when you cleared 'sub', the actual data got cleared and as there was only one copy of the data(with two references pointing to it), it cleared.

Now when you added to sub back , it got added to the same memory location which was pointed by both the references. Hence you see its copy in main.

Hope you understood.

answered Oct 2, 2014 at 4:31

Comments

1

you can clone the same object like this:

public static void main(String[] args) {
 ArrayList<List<Integer>> main = new ArrayList<List<Integer>>(); 
 ArrayList<Integer> sub=new ArrayList<Integer>();
 sub.add(1);
 sub.add(2);
 main.add(sub);
 sub=(ArrayList<Integer>) sub.clone();// clone here
 sub.clear(); 
 System.out.println("sub = "+sub);
 System.out.println("main = "+main);
 sub.add(5);
 sub.add(6);
 sub.add(7);
 main.add(sub);
 System.out.println("sub = "+sub);
 System.out.println("main = "+main);
 }

output :

sub = []
main = [[1, 2]]
sub = [5, 6, 7]
main = [[1, 2], [5, 6, 7]]
answered Oct 2, 2014 at 4:31

Comments

1

Assuming your Integer's are already in a collection that is the basis of your "Big Loop":

ArrayList<List<Integer> main_list = new ArrayList<>();
Collection<Collection<Integer>> mySubs = xxxx;
for (Collection<Integer> group : mySubs){
 List<Integer> sub = new ArrayList<>(group);
 main_list.add(sub)
}

All of the Collection classes have constructors that can take other Collection's

If you don't have an existing collection to start with, then you'll just need to use sub.clone().clear() or sub = new ArrayList<>()

answered Oct 2, 2014 at 4:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.