Java Tutorial
Java ArrayList
ArrayList is a resizable array. Prefer it over raw arrays when the length is not known up front.
add, get, size
new ArrayList<Integer>() starts empty. add appends, get(i) reads an index, and size() is how many elements you have so far — not a fixed capacity you chose at creation.
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList nums = new ArrayList<>();
nums.add(4);
nums.add(17);
System.out.println(nums.size() + " " + nums.get(1)); // 2 17
}
} Program to List
Declare the variable as List<...> and construct an ArrayList. That habit lets you swap implementations later and matches how most APIs are written.
Example
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List names = new ArrayList<>();
names.add("Ada");
names.add("Lin");
names.set(1, "Grace");
System.out.println(names); // [Ada, Grace]
}
} Loop and remove
for-each walks values when you do not need the index. To remove while iterating safely, use anIterator or loop by index carefully — removing inside a plain for-each throwsConcurrentModificationException.
Example
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List nums = new ArrayList<>();
nums.add(1);
nums.add(2);
nums.add(3);
nums.removeIf(n -> n % 2 == 0);
for (int n : nums) System.out.print(n + " ");
System.out.println(); // 1 3
}
} Use wrapper types (Integer, Double) in the type argument — generics cannot hold primitives. Autoboxing converts int ↔ Integer for you.
Generics keep types honest
The <Integer> is the element type. You cannot add a String to anArrayList<Integer> — javac stops you at compile time.
Example
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List nums = new ArrayList<>();
nums.add(10);
// nums.add("ten"); // error: incompatible types
int first = nums.get(0);
System.out.println(first);
}
} List.of(...) creates an immutable list — add / remove will throw. Use new ArrayList<>() when you need to grow or shrink.
Try It Yourself
Exercise: Build an ArrayList<String> of three cities, print size(), then print each city on its own line with for-each.
Show solution
import java.util.ArrayList;
import java.util.List;
List cities = new ArrayList<>();
cities.add("Berlin");
cities.add("Paris");
cities.add("Tokyo");
System.out.println(cities.size());
for (String c : cities) System.out.println(c); add grows the list; for-each visits each element in insertion order.
Key Takeaways
ArrayListgrows as youadd;size()is the live count.- Prefer declaring
List<T>and constructingArrayList. - Generics use wrappers (
Integer), not primitives. List.ofis immutable; use a realArrayListwhen you must mutate.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Maths
Growing list of marks
You rarely know how many marks will arrive. ArrayList grows. add appends; size is the count so far.
Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> marks = new ArrayList<>();
marks.add(72);
marks.add(88);
marks.add(91);
System.out.println(marks.size());
System.out.println(marks.get(1));
}
}