When declaring an ArrayList, the datatype stored in the container is specified inside of <>, and the data type must be the name of a class (no primitive data types)
The parameter to the method get represents the index in the ArrayList. The size of the ArrayList is the number of elements contained. If the ArrayList is initially empty, the size is 0.
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 one exception to this is if you assign the argument to reference a different ArrayList in memory.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> gamma = new ArrayList<>();
for (int x = 1; x <= 7; x++)
gamma.add(x*10);
update(gamma);
System.out.println(gamma);
}
public static void update(ArrayList<Integer> aList) {
for (int i = 0; i < aList.size()-1; i++) {
aList.set(i, aList.get(i + 1));
}
}
}
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> words = new ArrayList<>();
words.add("One");
words.add("Two");
int value = 6;
changeIt(words, value);
System.out.println(words + " " + value);
}
public void changeIt(ArrayList<String> list, int num) {
list = new ArrayList<String>();
num = 0;
list.add("Zero");
System.out.print(list);
}
}