2

So I basically put some Integer objects in an ArrayList x, then put some in an ArrayList y, then did y.addAll(x), but it seems to be only adding the first one and leaving out all the rest! Isn't it supposed to add ALL? I looked it up on Oracle and they showed what seemed to be the EXACT SAME EXAMPLE yet mine is not working properly. Here is my code:

ArrayList<Integer> x = new ArrayList<Integer>();
Integer a = 1;
Integer b = 2;
Integer c = 3;
x.add(a);
x.add(b);
ArrayList< Integer> y = new ArrayList< Integer>();
y.addAll(x);

yet y seems to only have 1 in it and missing the 2 and the 3.

What am I doing wrong?

EDIT: Yes, sorry, I know I didn't actually add c to x yet, but even when I do, and then print y, I still get [1] instead of [1,2,3]. This is what I don't understand.

import java.util.ArrayList;
public class SumArrayList {

    public static void main(String[] args) {

        ArrayList<Integer> x = new ArrayList<Integer>();
        Integer a = 1;
        Integer b = 2;
        Integer c = 3;
        x.add(a);
        x.add(b);
        System.out.println(x);
        //System.out.println(calculateSumArrayListHelper(x));

        ArrayList<Integer> y = new ArrayList<Integer>();
        y.addAll(x);
        System.out.println(y);
        //System.out.println(calculateSumArrayListHelper(y));
    }
}

Here is a pic of what I see: http://postimg.org/image/ms0y68nnh/

5
  • 3
    No repro here using your code Commented Apr 13, 2016 at 8:23
  • Can you show us the code you used to print all elements ? Commented Apr 13, 2016 at 8:24
  • 1
    I'm sure you get [1,2] not just 3 because you haven't added it Commented Apr 13, 2016 at 8:25
  • How do you know it only contains one element ? Commented Apr 13, 2016 at 8:27
  • Okay, here I'll just give you my entire code, and yeah I realize I didn't add 3, but it's still printing just [1] when I print y and [1,2] when i print x Commented Apr 13, 2016 at 8:28

3 Answers 3

2

With the picture you have posted it is clear. You are calling calculateSumArrayListHelper(x) before the addAll(). Inside that method you remove elements from the given list. So at the time you call y.addAll(x), elements have been removed from x.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my goodddddddd............................ I'm such a idiot. Thank you so much
2

In your code after executing y.addAll(x); y will contain only 2 elements [1,2]. you never add c to x:

ArrayList<Integer> x = new ArrayList<>();
Integer a = 1;
Integer b = 2;
Integer c = 3;
x.add(a);
x.add(b);
x.add(c); // add c to x
ArrayList< Integer> y = new ArrayList<>();
y.addAll(x);
System.out.println("size = " + y.size());

Will produce :

size = 3

And y will contain :

[1,2,3]

You are removing item from x in your method calculateSumArrayListHelper(). That's why you don't have the expected result.

Comment all calls to this method and check the content of y... It should be right.

3 Comments

Yeah this is what I don't understand. I literally have done this exactly, but when I print y, it just gives me "[1]" and not "[1,2,3]"
And printing y.size() what is the result?
when I print y.size() it says 1
0

Just put sop on last line.

System.out.println(y);

So that you can see the result.which is coming [1, 2] in my system. You are not getting 3 as because you never add it in X.

x.add(a);
x.add(b);
x.add(c);

Comments

Your Answer

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.