ArrayList empty

The syntax of the isEmpty[] method is:

arraylist.isEmpty[]

Here, arraylist is an object of the ArrayList class.

isEmpty[] Parameters

The isEmpty[] method does not take any parameters.

isEmpty[] Return Value

  • returns true if the arraylist does not contain any elements
  • returns false if the arraylist contains some elements

Example: Check if ArrayList is Empty

import java.util.ArrayList; class Main { public static void main[String[] args] { // create an ArrayList ArrayList languages = new ArrayList[]; System.out.println["Newly Created ArrayList: " + languages]; // checks if the ArrayList has any element boolean result = languages.isEmpty[]; // true System.out.println["Is the ArrayList empty? " + result]; // add some elements to the ArrayList languages.add["Python"]; languages.add["Java"]; System.out.println["Updated ArrayList: " + languages]; // checks if the ArrayList is empty result = languages.isEmpty[]; // false System.out.println["Is the ArrayList empty? " + result]; } }

Output

Newly Created ArrayList: [] Is the ArrayList empty? true Updated ArrayList: [Python, Java] Is the ArrayList empty? false

In the above example, we have created a arraylist named languages. Here, we have used the isEmpty[] method to check whether the arraylist contains any elements or not.

Initially, the newly created arraylist does not contain any element. Hence, isEmpty[] returns true. However, after adding some elements [Python, Java], the method returns false.

Learn to clear arraylist or empty an arraylist in Java. Clearing a list means to remove all elements from the list. It is same as reset the list to it’s initial state when it has no element stored in it.

To clear an arraylist in java, we can make use of two methods.

  1. ArrayList.clear[]
  2. ArrayList.removeAll[]

Both methods will finally empty the list. But there is a difference in how they perform the empty operation.

1. Clear arraylist with ArrayList.clear[]

Java program to clear an arraylist.

import java.util.ArrayList; import java.util.Arrays; public class ArrayListExample { public static void main[String[] args] throws Exception { ArrayList list = new ArrayList[Arrays.asList["a", "b", "c", "d", "e"]]; System.out.println[list]; list.clear[]; //clear the list System.out.println[list]; } }

Program output.

[a, b, c, d, e] []

2. Clear arraylist with ArrayList.removeAll[]

Java program to remove all elements of an arraylist with removeAll[] method.

Program output.

[a, b, c, d, e] []

3. Difference between clear[] vs removeAll[] methods

As I said before, both methods empty an list. But difference lies in how they make list clear. Let’s see the code for both methods to understand the actions they perform.

  1. clear[] method is simple. It iterates over list and assign null to each index in the list.public void clear[] { // clear to let GC do its work for [int i = 0; i < size; i++] elementData[i] = null; size = 0; }
  2. In removeAll[] method, it first check if element is present or not using contains[] method. If element is present then it is removed from the list. This happens for all the elements in the loop.

4. Conclusion

By going through the sourcecode of both methods, we can safely say that clear[] method give much better performance because of less number of statements it executes.

removeAll[] method lack in performance because of extra call to contains[] method.

But, still removeAll[] method is useful in cases such as merge two arraylists without duplicate elements.

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Let us know if you liked the post. That’s the only way we can improve.

ArrayList clear[] method is used to removes all of the elements from the list. The list will be empty after this call returns.

1. ArrayList clear[] syntax

clear[] method does simple thing. It iterates the backing array inside arraylist and assign all elements 'null' value and set the size attribute to '0'.

public void clear[] { modCount++; // clear to let GC do its work for [int i = 0; i < size; i++] elementData[i] = null; size = 0; }
  • Method parameter – none.
  • Method returns – void.
  • Method throws – none.

2. ArrayList clear[] example

Java program to make empty an arraylist using clear[] method.

import java.util.ArrayList; public class ArrayListExample { public static void main[String[] args] { ArrayList arrayList = new ArrayList[]; arrayList.add["A"]; arrayList.add["B"]; arrayList.add["C"]; arrayList.add["D"]; System.out.println[arrayList]; arrayList.clear[]; System.out.println[arrayList]; } }

Program output.

[A, B, C, D] []

3. ArrayList clear vs new

An empty arraylist has zero elements. A new arraylist also has zero elements. But there is differenece between them.

The difference between an empty and a new arraylist is the size of backing array. As clear[] method does not resize the backing array, so after clear method you may have a list which has backing array of a larger size [if list was pretty big before clear[] method was called].

Except above difference in capacity, there is no difference between both kind of lists.

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Let us know if you liked the post. That’s the only way we can improve.

To create an Empty ArrayList in Java, you can use new keyword and ArrayList constructor with no arguments passed to it.

Following is the syntax to create an empty ArrayList.

ArrayList myList = new ArrayList[];

Example 1 – Create an Empty ArrayList of Strings

In the following example, we shall create an empty ArrayList of Strings.

Java Program

import java.util.ArrayList; public class ArrayListExample { public static void main[String[] args] { ArrayList names = new ArrayList[]; } }

names is an empty ArrayList that can store String elements.

Example 2 – Create an Empty ArrayList of Objects

In the following example, we shall create an empty ArrayList that can store Car type objects.

Java Program

import java.util.ArrayList; public class ArrayListExample { public static void main[String[] args] { ArrayList cars = new ArrayList[]; } } class Car { public String name; public int miles; }

cars is an empty ArrayList that can store Car objects.

Conclusion

In this Java Tutorial, we have learned how to create an empty ArrayList in Java.

Test your Java Basics with our Java Quiz

➥ PDF Download - How to Create an Empty ArrayList in Java?

isEmpty[] method of java.util.ArrayList class is used for checking whether the list is empty or not. This method returns a boolean value.

public boolean isEmpty[]

It returns true if the list is empty otherwise it gives false.

Example

package beginnersbook.com; import java.util.ArrayList; public class IsEmptyExample { public static void main[String args[]] { //ArrayList of Integer Type ArrayList al = new ArrayList[]; //Checking whether the list is empty System.out.println["Is ArrayList Empty: "+al.isEmpty[]]; //Adding Integer elements al.add[1]; al.add[88]; al.add[9]; al.add[17]; //Again checking for isEmpty System.out.println["Is ArrayList Empty: "+al.isEmpty[]]; //Displaying elements of the list for [Integer num: al] { System.out.println[num]; } } }

Output:

Is ArrayList Empty: true Is ArrayList Empty: false 1 88 9 17

Video liên quan

Chủ Đề