0

I have read that when we remove an Object from ArrayList, its size doesn't decrease.

I tried the below code to test this, but found that size is decreased. Could anyone correct or explain this to me?

package collections;

import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {  
public static void main(String[] args) {  
    //Creating list of Books  
    List<Book> list=new ArrayList<Book>();  
    //Creating Books  
    Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);  
    Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);  
    Book b3=new Book(103,"Operating System","Galvin","Wiley",6);  
    //Adding Books to list  
    list.clear();
    list.add(b1);  
    list.add(b2);  
    list.add(b3); 
    list.contains(b3);
    int size= list.size();
    System.out.println(size);
    list.remove(b3);
    int size1= list.size();
    System.out.println(size1);      
}  
}  
mustaccio
  • 18,234
  • 16
  • 48
  • 57
Snigdha
  • 21
  • 1
  • 1
  • 1
    ***I have read that when we remove an Object from Arraylist ,its size doesn't decrease...*** where did you read that? – ΦXocę 웃 Пepeúpa ツ Feb 14 '17 at 15:06
  • 1
    Where did you read that? Perhaps you read something about arrays and not ArrayLists? – azurefrog Feb 14 '17 at 15:06
  • 2
    Maybe the underlying storage won't decrease its size (which would be an implementation detail), but the `size` method will always reflect the actual number of elements contained in the list as stated in the javadocs. – languitar Feb 14 '17 at 15:16

2 Answers2

8

You probably read that the size of the backing array used by the ArrayList doesn't decrease, which is true. If you put 100 elements in an ArrayList, it will have a backing array of at least 100 in size. Even after you remove() elements, the size of that array stays the same. To free up that memory, the trimToSize() method is available:

public void trimToSize()
Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.

From https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#trimToSize()

ndsilva
  • 309
  • 1
  • 8
0

There are size and capacity.

1) Size - total amount of elements in list. It will decrease after remove element.

    list.add(b1);  
    list.add(b2);  
    list.add(b3); 
    System.out.println(list.size());//size is 3
    list.remove(b3);
    System.out.println(list.size());//size is 2 after remove

2) Capacity - total amount of "cells" for elements in list. It will not decrease after remove element.

List<Book> list1 = new ArrayList<>(3); //there is 3 "cells" for books
List<Book> list2 = new ArrayList<>(); //there is 10 "cells" for books(default value)

You still can do this:

List<Book> list1 = new ArrayList<>(3); // 3 "cells" for books
list.add(b1); //capacity = 3; size = 1; 
list.add(b2); //capacity = 3; size = 2; 
list.add(b3); //capacity = 3; size = 3; 
list.add(b4); //capacity = (3 * 3) / 2 + 1 = 5; size = 4;

You can add 4 elements in list with 3 "cells" because JVM will autoincrease capacity of list when there are not enough capacity( formula = (oldCapacity * 3) / 2 + 1 ).

There is picture of next code:

List<String> list = new ArrayList<>();
list.add("0");
list.add("1");

You can read more about capacity there.

Community
  • 1
  • 1
Ihar Knyshau
  • 11
  • 1
  • 2