arraylist length java
aList.size()
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<String> 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
java list length
ArrayList<String> listOfBanks = new ArrayList<>();
int size = listOfBanks.size();
System.out.println("size of array list after creating: " + size);
ArrayList size() method in java
import java.util.ArrayList;
public class ArrayListSizeMethodExample
{
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>();
int s = al.size();
System.out.println("size of ArrayList: " + s);
al.add(25);
al.add(64);
al.add(73);
s = al.size();
System.out.println("size of ArrayList after adding elements: " + s);
al.clear();
s = al.size();
System.out.println("size of ArrayList after clearing elements: " + s);
}
}
ArrayList size() method in java
import java.util.ArrayList;
public class ArrayListSizeMethodExample
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
int s = al.size();
System.out.println("size of ArrayList: " + s);
al.add("red");
al.add("blue");
al.add("green");
s = al.size();
System.out.println("size of ArrayList after adding elements: " + s);
al.clear();
s = al.size();
System.out.println("size of ArrayList after clearing elements: " + s);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us