AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Java ArrayList: tutorial – IONOS

PARTAGEZ

The class ArrayList is one of the most often used data structures in Java. It allows you to dynamically modify and save a set of objects. In this tutorial, we introduce you to the syntax and methods related to the ArrayList class in Java.

What is the difference between ArrayList and Array classes in Java?

Java’s ArrayList class has dynamic size, which means that elements can be easily added or removed. Additionally, the ArrayList class is part of the Java Collections Framework but unlike the Array class, it is not native. You need to import it from library java.util.

The ArrayList is a suitable method when the length of the Java list is likely to vary. This is the case when saving objects, search or sort data, as well as the creation of lists or queues.

In comparison, the size of the Array data type is not changeable. The number of objects that an array must store must therefore be known before creation. The Array class is therefore suitable for managing a predefined quantity of primitive data typeslike int, float, char or boolean.

One of the disadvantages of the ArrayList class is the longer access time. While with an Array, a fixed storage space is reserved, this is not the case with an ArrayList. It is therefore important to take into account the respective advantages and disadvantages of these two classes and to choose the appropriate data structure for the corresponding use.

Java ArrayList Syntax

Before creating an ArrayList, the corresponding class must first be imported from the library java.util.

import java.util.ArrayList;

Java

The general syntax is as follows:

ArrayList<Type> arrayList= new ArrayList<>();

Java

Here, “Type” corresponds to the respective data type of the Java ArrayList.

Next, we create lists of strings (“String”) and integers (“Integer”).

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

Java

ArrayList<Integer> arrayList= new ArrayList<>();

Java

ArrayLists use envelope classes corresponding primitive data types so that they can be treated as objects. This is why we need to type “Integer” and not “int”.

Java ArrayList Method Examples

With the ArrayList class, operations such as adding or removing elements are not performed with Java operators, but with predefined methods. Below we present the most common ArrayList methods.

Add items

Once the ArrayList “colors”, of type “String”, has been created, we add various elements using the “.add()” method.

import java.util.ArrayList;

class Main {
  public static void main(String[] args){

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

    colors.add("blue");
    colors.add("red");
    colors.add("green");

    System.out.println("ArrayList: " + colors);
  }
}

Java

The result is as follows:

ArrayList: [blue, red, green]

Java

Delete items

To remove objects from a Java ArrayList, we use the “.remove()” method supplemented with the numeric indicator of the elements to be removed.

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> colors = new ArrayList<>();

    colors.add("blue");
    colors.add("red");
    colors.add("green");
    System.out.println("ArrayList: " + colors);

    String color = colors.remove(1);

    System.out.println("ArrayList: " + colors);
    System.out.println("Removed Element: " + color);
  }
}

Java

The result displays the modified ArrayList, as well as the deleted element:

ArrayList : [blue, green]
Removed Element: red

Java

As in the majority of programming languages, in Java, the count starts from the position 0. This is why the deleted element is the value “red” which corresponds to indicator 1.

Access elements of a Java ArrayList

Using the “.get()” function, we access an element located at a specific position.

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> clothes = new ArrayList<>();

    clothes.add("jacket");
    clothes.add("shirt");
    clothes.add("pullover");
    System.out.println("ArrayList: " + clothes);

    String str = clothes.get(2);

    System.out.print("Element at index 2: " + str);
  }
}

Java

The result is as follows:

ArrayList: [jacket, shirt, pullover]
Element at index 2: pullover

Java

Edit items

Using “.set()”, we set a new element at a specific position.

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> colors = new ArrayList<>();

    colors.add("blue");
    colors.add("red");
    colors.add("green");
    System.out.println("ArrayList: " + colors);

    colors.set(2, "yellow");

    System.out.println("Modified ArrayList: " + colors);
  }
}

Java

In the result, we now read “yellow” in position 2 and no longer “green”.

ArrayList: [blue, red, green]
Modified ArrayList: [blue, red, yellow]

Java

Determining the length of a Java ArrayList

The number of elements making up an ArrayList is easily calculated using the “.size()” method.

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> colors = new ArrayList<>();

    colors.add("blue");
    colors.add("red");
    colors.add("green");
    System.out.println(colors.size());

  }
}

Java

The result is as follows:

Sort and iterate through an ArrayList

To sort a Java ArrayList, you need to import the Collections class. For the iteration, we use a Java for-each loop. Each time the loop is executed, the corresponding element is indicated on the screen.

import java.util.ArrayList;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> ages = new ArrayList<Integer>();

    ages.add(20);
    ages.add(54);
    ages.add(17);
    ages.add(9);

    Collections.sort(ages);

    for (int i : ages) {
      System.out.println(i);
    }
  }
}

Java

The elements of the ArrayList are returned in ascending order:

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact