java easy list

h‮tt‬ps://www.lautturi.com
java easy list

In Java, the List interface is a part of the java.util package and is a subinterface of the Collection interface. It represents an ordered sequence of elements, and provides methods for inserting, deleting, and accessing elements in the list.

There are several classes that implement the List interface in the Java Standard Library, including the ArrayList, LinkedList, and Vector classes. These classes provide different implementations of the List interface and have different performance characteristics depending on the specific use case.

Here's an example of how to use the ArrayList class, which is a commonly used implementation of the List interface:

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

public class ListExample {
    public static void main(String[] args) {
        // Create a new ArrayList
        List<String> list = new ArrayList<>();

        // Add some elements to the list
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");

        // Access an element in the list
        String element = list.get(1);  // element is now "Item 2"

        // Iterate over the list
        for (String s : list) {
            System.out.println(s);  // Outputs: Item 1, Item 2, Item 3
        }

        // Remove an element from the list
        list.remove(1);  // Removes "Item 2" from the list

        // Check the size of the list
        int size = list.size();  // size is now 2
    }
}

This example creates a new ArrayList and adds three elements to it using the add() method.

Created Time:2017-11-03 15:57:11  Author:lautturi