Java
How to get the size of an ArrayList in Java
⏱️ 10 min de lecture
👁️ 2951 vues
Using the ArrayList method size() you can very easily determine the size of an ArrayList in Java. This method returns the current number of items in an ArrayList.
import java.util.ArrayList;
public class ArrayListSize {
public static void main(String[] args) {
//Create ArrayList
ArrayList arraylist = new ArrayList();
//add the elements to ArrayList
arraylist.add(1);
arraylist.add(11);
arraylist.add(22);
//ArrayList size before
System.out.println("Before: "+ arraylist.size());
arraylist.add(33);
//size of the ArrayList after adding an element
System.out.println("After: "+ arraylist.size());
}
}
Runtime:
Before: 3
after: 4
Commentaires (0)
Laisser un commentaire
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Chargement des commentaires...