Get Size of ArrayList and loop through elements in Java
Description
The following code shows how to get Size of ArrayList and loop through elements.
Example
/*w w w . j a v a2s .co m*/
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("java2s.com");
int totalElements = arrayList.size();
for (int index = 0; index < totalElements; index++)
System.out.println(arrayList.get(index));
}
}
The code above generates the following result.