ArrayList.isEmpty() Checking if any element is present inside the ArrayList.
It returns boolean value, true if the ArrayList is empty, false otherwise.
ArrayList<String> languages = new ArrayList<String>();
System.out.println(languages.isEmpty()); // true
languages.add("Java");
languages.add("PHP");
languages.add("Python");
System.out.println(languages.isEmpty()); // false
ArrayList<Integer> marks = new ArrayList<Integer>();
System.out.println(marks.isEmpty()); // true
marks.add(55);
marks.add(54);
marks.add(55);
marks.add(54);
System.out.println(marks.isEmpty()); // false
At the second line we have a blank ArrayList ( without any elements inside ) so the isEmpty() returns true. After this we have added elements by using add() , so the last line is returning true as the ArrayList is no longer empty.
« ArrayList Tutorials