xxxxxxxxxx
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> myList = List.of("apple", "banana", "orange");
int length = myList.size();
System.out.println("The length of the list is: " + length);
}
}
xxxxxxxxxx
Collection<Object> list = new ArrayList<>();
list.size();
// All collections use size()
// array use array. length
list.size()
xxxxxxxxxx
// Java program to Illustrate size() method
// of List class for Integer value
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty string list by
// declaring elements of string type
List<String> list = new ArrayList<String>();
// Populating List by adding string elements
// using add() method
list.add("Geeks");
list.add("for");
list.add("Geeks");
// Printing the List
System.out.println("Before operation: " + list);
// Getting total size of list
// using size() method
int size = list.size();
// Printing the size of list
System.out.println("Size of list = " + size);
}
}
xxxxxxxxxx
ArrayList<String> listOfBanks = new ArrayList<>();
int size = listOfBanks.size();
System.out.println("size of array list after creating: " + size);
xxxxxxxxxx
List<?> list = new ArrayList<>(); // Replace '?' with the type of objects stored in the list
int length = list.size();
System.out.println(length); // This line is optional, used to display the length
xxxxxxxxxx
List<String> myList = new ArrayList<>();
myList.add("Element 1");
myList.add("Element 2");
myList.add("Element 3");
int size = myList.size();
System.out.println("Size of the list: " + size);
xxxxxxxxxx
import java.util.List;
public class ListLengthExample {
public static void main(String[] args) {
List<String> myList = List.of("apple", "banana", "orange");
int length = myList.size();
System.out.println("Length of the list: " + length);
}
}