Java ArrayList isEmpty() method with example

Java ArrayList isEmpty() method Returns true if this list contains no elements.

Method Signature

public boolean isEmpty()

Specified by:

isEmpty method in interface Collection

Specified by:

isEmpty method in interface List

Overrides

Overrides: isEmpty method in class AbstractCollection

Returns:

true if this list contains no elements

ArrayList isEmpty() method implementation

 public boolean isEmpty() {
        return size == 0;
    }

Java ArrayList isEmpty() method example

package com.ehowtonow.java.util.arraylist;

import java.util.ArrayList;
import java.util.Iterator;

public class IsEmptyExample {

	public static void main(String[] args) {

		// create empty array list with Integer type
		ArrayList<Integer> numericList = new ArrayList<Integer>(2);

		System.out.println("List isEmpty check : " + numericList.isEmpty());

		// add() method to add Integer element in the list
		numericList.add(10);
		numericList.add(15);
		numericList.add(20);
		numericList.add(25);
		numericList.add(30);

		System.out.println("List isEmpty check after element added : " + numericList.isEmpty());

		// print the values of list
		for (Integer numbers : numericList) {
			System.out.println(" Numbers : " + numbers);
		}

		
	}

}

Output

List isEmpty check : true
List isEmpty check after element added : false
Numbers : 10
Numbers : 15
Numbers : 20
Numbers : 25
Numbers : 30


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java ArrayList add(E e) method with example
  2. Java ArrayList get() method with example
  3. Java ArrayList indexOf() method with example
  4. Java ArrayList add(int index, E element) method with example
  5. Java ArrayList removeAll() method with example
  6. Java ArrayList remove(Object o) method with example
  7. Java ArrayList clear() method with example
  8. Java ArrayList contains() method with example
  9. Java ArrayList forEach() method with example
  10. Java ArrayList remove() method with example
  11. Java ArrayList addAll(Collection< ? extends E> c) method with example
  12. Java ArrayList ensureCapacity() method with example
  13. Java ArrayList clone() method with example
  14. Java ArrayList isEmpty() method with example
  15. Java ArrayList addAll(int index, Collection c) method with example

Be the first to comment

Leave a Reply