All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Clear An Array In Java

Last Updated : Mar 11, 2024

How To Clear An Array In Java

In this article we will show you the solution of how to clear an array in java, in order to understand more about the method, let's look at the syntax for the clear() method from the ArrayList. In the clear() method, all elements of the array are nullified as they are iterated through.

The ArrayList can be cleared by using ArrayList.clear() or by removing all items with ArrayList.removeAll().

A nutshell, clear() is faster since it only resets the underlying array reference to null, while removeAll() also performs additional calculations.

We will now discuss the idea of how to clear an array in java with an example.

Step By Step Guide On How To Clear An Array In Java :-

public boolean removeAll(Collection << ? > c) {
    return batchRemove(c, false, 0, size);
}
boolean batchRemove(Collection << ? > c, boolean complement,
    final int from, final int end) {
    Objects.requireNonNull(c);
    final Object[] es = elementData;
    int r;
    // Optimize for initial run of survivors
    for (r = from;; r++) {
        if (r == end)
            return false;
        if (c.contains(es[r]) != complement)
            break;
    }
    int w = r++;
    try {
        for (Object e; r < end; r++)
            if (c.contains(e = es[r]) == complement)
                es[w++] = e;
    } catch (Throwable ex) {
        // Preserve behavioral compatibility with AbstractCollection,
        // even if c.contains() throws.
        System.arraycopy(es, r, es, w, end - r);
        w += end - r;
        throw ex;
    } finally {
        modCount += end - w;
        shiftTailOverGap(es, w, end);
    }
    return true;
}
  1. The method removeAll() takes a Collection as a parameter, and returns a boolean value. It calls another private method batchRemove() to remove all the elements in the given Collection from the current collection.
  2. The batchRemove() method first checks if the given collection is null or not using Objects.requireNonNull(). It then gets the elementData array of the current collection.
  3. The method then starts iterating from the 'from' index to the 'end' index. If the element at the current index is present in the given collection, and if the 'complement' flag is not set, it breaks the loop. This is because we are trying to remove the elements in the given collection, and if the flag is not set, we need to remove the elements that are present in the given collection and leave the rest.
  4. Once it finds the first element to remove, it increments the index 'w' to start copying elements from the current index 'r'. It then iterates from the next index 'r' to the end of the collection. If the element at the current index 'r' is not present in the given collection, and if the 'complement' flag is set, it copies the element to the index 'w', and increments it.
  5. If there is any exception thrown during this operation, it is caught, and the remaining elements are copied to the position 'w' using System.arraycopy(). The exception is then rethrown.
  6. Finally, the method updates the modCount of the current collection, which keeps track of the number of structural modifications to the collection, and calls another private method shiftTailOverGap() to shift elements from the end to the position 'w'. It returns true if the collection was modified, otherwise false.

Conclusion :-

As a result, we have successfully learned how to clear an array in java with an example.

A discussion of the clear() method of the ArrayList is intended in this article.

In this article, we discussed when it is better to use the clear() method versus creating and assigning an ArrayList from scratch.

ArrayList's clear() and removeAll() methods will be compared in this article.

I hope this article on how to clear an array in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪