-1

When we create an ArrayList and check its size, it gives the value as 0. However, when we add an element and remove that element, and then check its size, it throws exception.

What is the internal implementation in remove() method, that it changes the definition of an empty list?

Code below gives output as 0.

List<Integer> list2 = new ArrayList<Integer>();
System.out.println(list2.size());

Code below throws Exception:

List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
System.out.println(list1.size());
list1.remove(1);
System.out.println(list1.size());
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
Deca
  • 915
  • 8
  • 16
  • 1
    I would expect the `remove(1)` to throw an exception at the very least. Are you sure it's `size` that's throwing? – Carcigenicate Aug 02 '18 at 13:27
  • Where did you get the message on the remove or when outputting the size the second time? – user12346352 Aug 02 '18 at 13:27
  • You should always first check the documentation of a method before using it. Sometimes your expectation and the intention of the author of the method do not match like in this case here. – BluesSolo Aug 02 '18 at 13:32

6 Answers6

3

There are two remove methods in List. There is remove(int index) and there is remove(Object o).

If you call remove(1), it resolves to remove(int index), and tries to remove the item at index 1 in your list (which is out of bounds).

If you want to remove the object 1 from your list, make sure the compiler knows you mean remove(Object), by calling

remove((Integer) 1)

Since an Integer is an object, this will call remove(Object).

khelwood
  • 46,621
  • 12
  • 59
  • 83
1

Your confusion is arising because List#add() adds the parameter passed to it, initially to the first position, which is at index zero. On the other hand, List#remove(int index) removes the item at the index you specify. Since you are specifying index one, the code fails with an exception.

This is probably what you intended to do:

List<Integer> list1 = new ArrayList<>();
list1.add(1);
System.out.println(list1.size());
list1.remove(0);
System.out.println(list1.size());  // should print 0, with no exception
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
0
remove(int index)

index, not value...

Eugene
  • 102,901
  • 10
  • 149
  • 252
0

list1.remove(1)

You are trying to remove an element from index 1, not the value 1. hence the array index out of bounds error.

0

list1.add(1); will add 1 to arraylist list1 but in

list1.remove(1);

will remove the value in the index 1(the second position, as ArrayList index starts from 0 ) of the array list.

make it

list1.remove(0)

and will work.

Vawani
  • 349
  • 6
  • 22
0

There is problem of your logic..

List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
System.out.println(list1.size());
list1.remove(1);
System.out.println(list1.size());

you add the integer value to list.so it prints the size as 1.but then you removed the value from list. when you removed something from list, you can call remove() with int index, this is one method. so you must insert the index of related value, not the value. then you can see there are no values which belongs to index number 1. only exists index number 0. so it will throws an array index out of bound exception. for more details, oracle docs