java list length code example
Example 1: get number of items in arraylist java
import java.util.ArrayList;
/*
* Java Program to find the length of ArrayList. *
*/
public class ArrayListDemo{
public static void main(String[] args) {
System.out.println("Welcome to Java Program to find the length of array list");
ArrayList listOfBanks = new ArrayList<>();
int size = listOfBanks.size();
System.out.println("size of array list after creating: " + size);
listOfBanks.add("Citibank");
listOfBanks.add("Chase");
listOfBanks.add("Bank of America");
size = listOfBanks.size();
System.out.println("length of ArrayList after adding elements: " + size);
listOfBanks.clear();
size = listOfBanks.size();
System.out.println("size of ArrayList after clearing elements: " + size);
}
}
Output
Welcome to Java Program to find length of array list
the size of array list after creating: 0
the length of ArrayList after adding elements: 3
the length of ArrayList after clearing elements: 0
Example 2: java list length
ArrayList listOfBanks = new ArrayList<>();
int size = listOfBanks.size();
System.out.println("size of array list after creating: " + size);
Example 3: length of list in java
a.size() where a is list
Example 4: hwo to get teh length of a arylist java
aList.size()
Example 5: length and length() method in java
length() : In String class we have length() method which is used
to return the number of characters in string.
Ex : String str = “Hello World”;
System.out.println(str.length());
Str.length() will return 11 characters including space.
length : we have length instance variable in arrays which will
return the number of values or objects in array.
For example :
String days[]={” Sun”,”Mon”,”wed”,”thu”,”fri”,”sat”};
Will return 6 since the number of values in days array is 6
Example 6: size list java
Before operation: [1, 2, 3, 4, 5]
Size of list = 5