Utilizando size()
método de ArrayList
clase que podemos fácilmente determinar el tamaño de ArrayList. Este método devuelve el número de elementos de ArrayList.
public int size()
Ejemplo:
package beginnersbook.com; import java.util.ArrayList; public class Details { public static void main(String [] args) { ArrayList<Integer> al=new ArrayList<Integer>(); System.out.println("Initial size: "+al.size()); al.add(1); al.add(13); al.add(45); al.add(44); al.add(99); System.out.println("Size after few additions: "+al.size()); al.remove(1); al.remove(2); System.out.println("Size after remove operations: "+al.size()); System.out.println("Final ArrayList: "); for(int num: al){ System.out.println(num); } } }
Producción:
Initial size: 0 Size after few additions: 5 Size after remove operations: 3 Final ArrayList: 1 45 99