Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add(). Elements not yet added do not exist until explicitly inserted.
Passing as argument - a copy of the reference to the instantiated ArrayList is passed to the method. This means that any changes made to the elements inside the method persist outside the method. The exception is if the argument is assigned to reference a different ArrayList inside the method.
ArrayList<Integer> alpha = new ArrayList<>();
alpha.add(2); alpha.add(4); alpha.add(6); alpha.add(8);
alpha.add(10); alpha.add(12); alpha.add(14);
int sum = 0;
for (int k = 0; k < alpha.size(); k+=2)
sum = sum + alpha.get(k);
ArrayList<Integer> beta = new ArrayList<>();
for (int i = 0; i < 5; i++) {
beta.add(0);
}
int sum = 0;
for (int k = 0; k < beta.size(); k++)
beta.set(k, 2 * k + 1);
int x = 3;
for (int k = 1; k < beta.size(); k++)
sum += beta.get(k);