Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
public class AddAndRemoveCollection {
    public static void main(String[] args) {
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(4);     p(ids);
        ids.add(3);
        ids.add(2);     p(ids);
        ids.remove(0);     p(ids);
        ids.add(1);     p(ids);
        ids.remove(new Integer(0));     p(ids);
        ids.add(1, 6);    p(ids);
        ids.remove(new Integer(6));    p(ids);
        ids.remove(2);    p(ids);
    }
    private static void p(List<Integer> ids) {
        System.out.print(ids);
    }
}


[4][4, 3, 2][4, 3, 2][4, 3, 2, 1][4, 3, 2, 1][4, 6, 3, 2, 1][4, 3, 2, 1][4, 3, 1]
[4][4, 3, 2][3, 2][3, 2, 1][2, 1][2, 6, 1][2, 1][2]
[4][4, 3, 2][3, 2][3, 2, 1][3, 2, 1][3, 6, 2, 1][3, 2, 1][3, 2]
Compilation Error
Runtime Error

Doubts

Problems

Topic: Java ArrayList

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App