import java.util.ArrayList; import java.util.List; public class MyList { public static void main(String[] args) { ListmyList = new ArrayList<>(); myList.add("apple"); myList.add("banana"); myList.add("orange"); System.out.println("The size of the list is: " + myList.size()); } }
import java.util.LinkedList; import java.util.List; public class MyList { public static void main(String[] args) { ListIn this example, we create a new LinkedList of type Integer and add ten elements to it using a for loop. Then, we use the size() method to check if the list is longer than 5 elements and print a message to the console if it is. The package library for the List class is java.util.myList = new LinkedList<>(); for (int i = 0; i < 10; i++) { myList.add(i); } if (myList.size() > 5) { System.out.println("The list is too long."); } } }