DEV Community

Indumathy
Indumathy

Posted on

ArrayList

ArrayList Class

ArrayList is one of the most commonly used classes in Java.
It implements the List interface.

Package: java.util

Syntax
ArrayList name = new ArrayList();

Features of ArrayList

  • Allows duplicate values
  • Maintains insertion order
  • Allows multiple null values
  • Dynamic size (it grows automatically)

Difference Between remove() and clear()
remove()
Removes a specific element from the list.
clear()
Removes all elements from the list.

Iterating Through List

We can use a for-each loop to iterate through elements.

for(String name : list){
System.out.println(name);
}

Let's See some common methods:
Lets create the student name list by ArrayList and make changes.

1.add()

Add Student Names Using ArrayList

import java.util.ArrayList;
import java.util.List;

public class Demo {

    public static void main(String[] args) {

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

        std.add("Hari");
        std.add("Indu");
        std.add("Kevin");

        System.out.println(std);

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

[Hari, Indu, Kevin]

2. add(index,"name")

Used to add name in specific place or index.

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

        std.add("Hari");
        std.add("Indu");
        std.add("Kevin");
        std.add(0,"Arul");
        System.out.println(std);
Enter fullscreen mode Exit fullscreen mode

Output:
[Arul, Hari, Indu, Kevin]

3.get(index)

Used to print the student name in given index.

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

        std.add("Hari");
        std.add("Indu");
        std.add("Kevin");

        System.out.println(std.get(0));
Enter fullscreen mode Exit fullscreen mode

output:
[Hari]

4.set(index,name)

Used to update or replace the student name in given index.

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

        std.add("Hari");
        std.add("Indu");
        std.add("Kevin");
        std.set(0,"abc");
        System.out.println(std);
Enter fullscreen mode Exit fullscreen mode

output:
[abc, Indu, Kevin]

5.remove(index)

Used to remove the name from specific index

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

        std.add("Hari");
        std.add("Indu");
        std.add("Kevin");
        std.remove(0);
        System.out.println(std);
Enter fullscreen mode Exit fullscreen mode

output:
[Indu, Kevin]

6.remove(value)

Used to remove particular student name.

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

        std.add("Hari");
        std.add("Indu");
        std.add("Kevin");
        std.remove("Indu");
        System.out.println(std);
Enter fullscreen mode Exit fullscreen mode

output:
[Hari, Kevin]

7.size()

Used to count the number of students

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

        std.add("Hari");
        std.add("Indu");
        std.add("Kevin");

        System.out.println(std.size());
Enter fullscreen mode Exit fullscreen mode

output:
[3]

8.contains(value)

Used to check if a student name is present in the list.

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

        std.add("Hari");
        std.add("Indu");
        std.add("Kevin");

        System.out.println(std.contains("abc"));
Enter fullscreen mode Exit fullscreen mode

output:
[false]

9. isEmpty()

Used to check whether the list is empty or not.

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

System.out.println(std.isEmpty());
Enter fullscreen mode Exit fullscreen mode

Output:
[True]

10. clear());

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

std.add("Indu");
std.add("Arul");
std.clear();
System.out.pritnln(std);


Enter fullscreen mode Exit fullscreen mode

Output:
[]

Top comments (0)