ArrayList in Java

In any programming language, a very significant concept is used to store the data named Arrays. The arrays are static/fixed in size and suitable for fixed-length data structures. But what if someone wants to add or remove the items dynamically? Well in Java, we can utilize the concept of ArrayList which allows us to store the data dynamically and is resizable.

This write-up presents a comprehensive guide for the following concepts regarding Java ArrayLists:

So, let’s get started!

What is an ArrayList

It is a predefined class in java that belongs to java.util package. The ArrayList has the same purpose as built-in arrays i.e. storing data. But It allows us to create resizeable arrays and it changes its size dynamically.

How to Work with ArrayList

In order to work with an ArrayList, we have to follow the following key points:

The very first thing that we have to do is “import” the ArrayList class from the java.util package, and to do so we have to add the following line in our project:

import java.util.ArrayList;

As ArrayList is a class, so, to work with an ArrayList we have to create an object of the ArrayList Class.

ArrayList<String> subjects = new ArrayList<String>();

The above snippet creates an string type object of the ArrayList class named “subjects” to store string values.

How to Add Items in ArrayList

The ArrayList class provides numerous methods and by using these methods we can achieve different functionalities. For instance, the ArrayList class provides an add() method to add new items in an ArrayList. The below example will provide a profound understanding of how to add items in an ArrayList.

Example

The below code snippet first imports the Arraylist and then creates the object of the ArrayList class. Next, we add multiple items to the ArrayList using the object of the ArrayList class:

import java.util.ArrayList;

public class ArrayListExample {

public static void main(String[] args) {

ArrayList<String> subjects = new ArrayList<String>();

subjects.add("Computer Science");

subjects.add("Mathematics");

subjects.add("Chemistry");

subjects.add("Biology");

subjects.add("Physics");

System.out.println(subjects);

}

}

The below-given snippet will provide the complete code along with its output:

The output verifies that all the items are successfully added to the list.

How to Access Items in ArrayList

The ArrayList in Java provides another handy method named get() that can be used to access the individual item of the ArrayList. It accesses the items on the basis of index number.

Example

The below-given line of code will let you understand how to access an item of the ArrayList:

System.out.println(subjects.get(3));

In the above snippet, we utilize the get() method with the object of the ArrayList class. Within the get method, we provide a value 3 which determines that access the value that is present at index 3.

As in ArrayList, indexing starts from the index zero so in this example index 3 carries a value “Biology”. So, the output verifies that the get() method access the appropriate value.

How to Remove Items from an ArrayList

You want to delete some unnecessary items from the list, no worries! ArrayList provides the remove() that can be utilized to remove the items from a list. Its syntax will be similar to the add() method i.e. we have to provide the index number of the item in the remove() method.

Example

Let’s assume we want to delete the second item of the ArrayList then our code will go like this:

subject.remove(1);

The complete code, as well as its respective output, is provided in the below-given screenshot:

The above output verified that the remove() method successfully deleted the desired item.

But what if someone wants to delete all the items of the list, deleting all the items individually will be a time taking process. Do we have a method that can delete all the items simultaneously?

The ArrayList class provides a clear() method to remove all the items of the list, so, let’s experiment with it to see how clear() works

subjects.clear();

The above snippet utilizes the clear() method with the object of the ArrayList class, let’s see the output to understand how it works:

The above output shows that all the items from the list have been deleted.

How to Modify/Change Items in ArrayList

In Java, the items of the ArrayList can be modified/changed with the help of the set() method. It will take two values/parameters: one for the index number and the second for the item you want to insert.

Example

In the below-given snippet, we pass 2, and English in the set() method which will replace the value of the second index with “English”

subjects.set(2, "English");

The complete code and output are provided in the following figure:

The output verifies that the set() method has successfully modified the value of the second index.

How to Find the Size of ArrayList

We can use the size() method to find how many items are there in the ArrayList.

Example

In this example we utilize the size() method to find the size of the list:

subjects.size();

The below-given snippet shows how the size() method works with the ArrayList:

The output authenticates the working of the size() method.

Iterating Through Arraylist

We can iterate through an ArrayList using the for-each loop, for loop, for-each method, etc.

Example

In the below-given code snippet, we utilize the for-each loop to traverse each item of the ArrayList:

for (String i : subjects) {

System.out.println(i);

}

The complete code and along with its output is shown below:

The output verifies that for-each loop iterates through each item of the list.

How to Sort an Arraylist

java.util provides a very useful class named Collections which provides numerous methods that can be used to perform different functionalities. For instance, we can sort the Arraylist alphabetically or numerically with the help of the sort() method of the Collection class.

Example

In the below code snippet, we utilize the sort() method to sort the subjects in alphabetical order:

Collections.sort(subjects);

The below-given snippet shows the complete code and its respective output:

The output verifies that the sort() method provides a sorted list.

Conclusion

In Java, the ArrayList class can be utilized to create a dynamic/variable length data structure. In order to work with Arraylist firstly we have to import the ArrayList class of java.util package into our project. The ArrayList class provides a wide range of methods to perform different functionalities e.g. add() method is used to add items in a list, remove() method is used to delete an item from a list, the sort() method is used to sort the ArrayList in numeric or alphabetic order. This write-up provides a detailed guide for what is an ArrayList and how to work with ArrayList in Java.



from https://ift.tt/qKozGWO

Post a Comment

0 Comments