How to add/remove new elements to an array in Java?

Java beginners often ask how to add new elements to an array? or how to remove elements from an array in Java? is that possible in Java? Well, no, it's not possible in Java because arrays length cannot be changed once created. Which means neither you can add new elements nor you can remove an old one. All you can do is that reassign values to the differnet bucket. For example, if you have a String array of length 3 e.g. {"java", "is", "great"} then you can just replace the individual index to make it like {"Python", "is", "great"} by replace "java" with "Python" at first index, but you cannot add or remove elements.

An important thing to remember is that when we say we add an element into an array, we mean that it's length increased by one and when deleting an element, it's length should go down by 1, which is not possible because length of the array is constant and cannot be changed once created. That's also the main difference between an array and linked list data structure.

Then the bigger question comes? What do we do? if we have an array of active user and new user logs in how do we store that into an array? how do we remove a user when he logs out.

Well, you can use ArrayList instead of an array, which allows you to add or remove elements. It's like a dynamic array which can grow and shrink in size if needed.

Btw, If you are a beginner then you should go through a comprehensive course like The Complete Java Master Class for Beginners to learn about essential classes from JDK, especially the Java Collection Framework.

Here is a code example to add and remove elements from an ArrayList in Java:

import java.util.ArrayList;

public class Helloworld {

  public static void main(String[] args) {

    // create an arraylist
    ArrayList<String> activeUsers = new ArrayList<>();
    System.out.println("ArrayList initially: " + activeUsers);
    // add users using add() method
    System.out.println("ArrayList after adding one element: " + activeUsers);

    System.out.println("ArrayList after adding two elements: " + activeUsers);

    System.out.println("ArrayList after adding three elements: " + activeUsers);

    // remove users using remove() method
    System.out.println("ArrayList after removing one element: " + activeUsers);

    System.out.println("ArrayList after removing two elements: " + activeUsers);

    System.out.println("ArrayList after adding three elements: " + activeUsers);

ArrayList initially: []
ArrayList after adding one element: [rohasa]
ArrayList after adding two elements: [rohasa, chibaka]
ArrayList after adding three elements: [rohasa, chibaka, sakasia]
ArrayList after removing one element: [chibaka, sakasia]
ArrayList after removing two elements: [sakasia]
ArrayList after adding three elements: []

From the output, you can see that the ArrayList changes after you add or remove elements. Their size also changes e.g. initially when you create an ArrayList the size is zero but once you add one element is incremented to 1 and goes up to 3 and then you start removing elements its size started to decrease and finally become zero one all elements were removed.

A size is a number of elements in ArrayList, you can get this by calling the size() method. It's very different from the length of Array, which never changes after you create the array.

That's all about how to add/remove elements into an array in Java. As I said, it's not possible because the length of the array cannot be changed. Apart from using ArrayList, a kind of dynamic array, only another option you have is to create a new array everytime you add or remove an element and copy elements from the old array to the new one, which is not an ideal solution.

It would result in so many temporary array object. Also copying elements from one array to another is very expensive operation especially for large arrays. In short, use ArrayList if you need a dynamic array in Java.

Thanks for reading this article so far. If you like this article and my explanation then please share with your friends and colleagues. If you have any questions or doubt then please drop a note. 

No comments:

Post a Comment