confused about using lists as method parameter.

    public static void removeFirst(ArrayList<Integer> numbers) {
        if (numbers.size() == 0) {
            return;
        }

        numbers.remove(0);
    }

    // main method

    ArrayList<Integer> numbers = new ArrayList<>();
    numbers.add(3);
    numbers.add(2);
    numbers.add(6);
    numbers.add(-1);

    System.out.println(numbers);

    removeFirst(numbers);

    System.out.println(numbers);

    removeFirst(numbers);
    removeFirst(numbers);
    removeFirst(numbers);

    System.out.println(numbers);

This came up on one of the lessons for MOOC and I'm having trouble understanding the output of this example. The output is as follows:

[3, 2, 6, -1]

[2, 6, -1]

[]

why is there an empty array for the third line?

Posted Mar 12, 2020, 5:56 AM UTC · edited

26
Loading comments…