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);
}
}