logo CodeStepByStep logo

arrayListMysteryInsert

Language/Type: Java ArrayList collections

What are the contents of the list after the following code executes?

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 5; i++) {
    list.add(2 * i);         // [2, 4, 6, 8, 10]
}
int size = list.size();
for (int i = 0; i < size; i++) {
    list.add(i, 42);    // add 42 at index i
}
System.out.println(list);
list contents

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.