Using List in Java

In Java, the List is an interface within the Java Collections Framework (JCF), enabling ordered collections with index-based operations. This interface allows adding, fetching, modifying, and deleting items from the list. To utilize a List, you should declare and instantiate a List object using the pattern: List<Type> listName = new ArrayList<>(), wherein "Type" denotes the data type of items to be stored. The add method helps in appending elements to the list, while the get method retrieves items based on their index. To modify an item, the set method is used, and to discard items, the remove method comes in handy, allowing removal based on either the item's value or its index. The size method offers the count of items present in the list. The contains method is employed to determine if a particular item exists in the list. In our Java List Example, we assemble a List, populate it, make alterations, and present the outcomes. Click Execute to run the Java List Example online and see the result.
Using List in Java Execute
import java.util.List;
import java.util.ArrayList;

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


        fruits.set(1, "Blueberry");


        System.out.println("Fruits: " + fruits);
Updated: Viewed: 42 times