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 intInteger 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

  • ArrayList grows as you add; size() is the live count.
  • Prefer declaring List<T> and constructing ArrayList.
  • Generics use wrappers (Integer), not primitives.
  • List.of is immutable; use a real ArrayList when 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));
  }
}

FAQ: Java ArrayList

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java arraylist examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java arraylist in this Java Java lesson (Java ArrayList).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.