The ArrayList class

Let's get started with the ArrayList class, which implements the List interface. Create a new class and name it arrayListexample. We will first look at the methods present in ArrayList, and then we'll discuss the difference between an array and ArrayList.

We start by declaring ArrayList as follows. If you hover over ArrayList in your IDE, you'll see a suggestion telling you to import java.util for ArrayList:

package coreJava;

public class arrayListexample {

public static void main(String[] args) {

ArrayList a=new ArrayList();

}
}

Once you do this, it'll still show a suggestion for ArrayList, and if you hover over it, it will suggest adding argument types. To remove this suggestion, you can pass an argument type to ArrayList, such as Integer or String:

        ArrayList<String> a=new ArrayList<String>();
a.add("rahul");
a.add("java");

After passing the argument type, you can easily add some string instances by using a. and it'll show you a list of different types supported by ArrayList. For ArrayList, we didn't define a specific array size, whereas when you see in arrays, we have explicitly defined a size. In arrays, once we define the size, you cannot decrease or increase the size. But in ArrayList, you could add or delete elements anytime from the list, it is a dynamic size array. This is one of the basic differences between array and ArrayList.

If we want to print this ArrayList, we can simply do that by adding the following line of code:

        System.out.println(a);

On running, it prints [rahul, java]. But if you want to print this in arrays, we need to write a for loop. We add another object and this time we specify the index where we want the string to go:

        a.add("rahul");
a.add("java");
System.out.println(a);
a.add(0, "student");
System.out.println(a);

When we print this, it gives the following output:

[rahul, java]
[student, rahul, java]

You can see that in the second line, student is added before rahul in the list as we have specified its index as 0.

If we want to remove an entry from the list, we can do that by adding the following lines of code:

        a.remove(1);
a.remove("java");

The first line of code will remove the entry from the list present at the first index, whereas the second line will find the string in the list and remove it. If you want to get the entry for a specific index, you can do that using the get method:

       a.get(2);

The preceding line of code will print java as the output, as it is the element present at index 2.

Let's say you have a list of 50 elements, and you need to find out whether a particular string/integer is present in that list. If you were to go with arrays, you would have to create a for loop and find out whether the element is present, but in ArrayList, we have a contains method that checks the entire list for us and gives the output in the form of true or false:

        System.out.println(a.contains("java"));

This will print the output as true as the element is present in our list; if you change it to, for example, testing, it will return the value as false as it is not present in our list.

Another useful method present in ArrayList is the indexOf method. If we want to find the index value of a particular element from the list, we can know that by using indexOf:

        System.out.println(a.indexOf("rahul"))

This will return the index number of this string.

Now, if we want to check whether the array is empty, we can do that using the isEmpty method in ArrayList, which will return the value as true or false:

        System.out.println(a.isEmpty());

This will return the value as false as our list is not empty.

The last and most important method in ArrayList is the size method, which returns the length of the list:

        System.out.println(a.size());

 One more thing you need to know about ArrayList is that all the classes that implement the List interface can accept duplicate values. We know the classes that extend List in the collection interface: ArrayListLinkedList, and vector. And all these classes can accept duplicate values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.204.140