JAVA — CHAPTER 6

Arrays and ArrayLists · Concept Cheat Sheet
Primitive vs Reference Declaring Arrays Multidimensional Pass-by-Value ArrayList
1 PRIMITIVE TYPES vs. REFERENCE TYPES

Primitive types

  • int, double, char, boolean
  • Variable holds the actual value directly
  • Fixed, predefined set (8 total)

Reference types

  • Arrays, Strings, and every object are reference types
  • Variable holds a reference (address) to the object in memory
  • Created with new, default value is null
2 DECLARING & CREATING ARRAYS
int[] numbers = new int[5]; // 5 zeros int[] grades = {87, 68, 94, 100, 83}; // array initializer numbers[0] = 10; // assign element System.out.println(numbers.length); // 5 — a field, not a method
870
681
942
1003
834

Indices always start at 0 and go to length−1.

3 WORKING WITH ARRAY ELEMENTS

Totaling elements

int total = 0; for (int i=0; i<grades.length; i++) total += grades[i];

Using elements as counters

An array like int[] freq = new int[7] can tally how many times each face of a die appears — freq[face]++.

Enhanced for (for-each)

for (int g : grades) { total += g; }
4 PASSING ARRAYS TO METHODS & EXCEPTION HANDLING

Pass-by-value vs pass-by-reference

Java is always pass-by-value — but for arrays/objects, the "value" passed is the reference itself. So a method can modify the array's elements, but reassigning the parameter inside the method does not affect the caller's variable.

try { System.out.println(grades[10]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Invalid index: " + e.getMessage()); }
5 MULTIDIMENSIONAL ARRAYS
int[][] grid = { {1, 2, 3}, {4, 5, 6} }; System.out.println(grid[1][2]); // 6

Notes

  • An array of arrays — rows can even have different lengths ("ragged" arrays)
  • Access with array[row][col]
  • Traverse with nested for loops (outer = rows, inner = columns)
6 VARARGS, COMMAND-LINE ARGS & CLASS Arrays

Variable-length arguments

static int sum(int... nums){ int t=0; for(int n: nums) t+=n; return t; }

Command-line arguments

public static void main(String[] args)args holds whatever was typed after the program name.

java.util.Arrays

  • Arrays.sort()
  • Arrays.toString()
  • Arrays.binarySearch()
  • Arrays.fill()
7 OBJECTS-NATURAL CASE STUDY: INTRO TO ArrayList
ArrayList<String> names = new ArrayList<>(); names.add("Amit"); names.add("Priya"); names.remove("Amit"); System.out.println(names.size());

Arrays vs. ArrayList

  • Arrays have a fixed size; ArrayList grows/shrinks dynamically
  • ArrayList is part of the Collections Framework (java.util)
  • Stores only objects — primitives are auto-boxed (e.g. int → Integer)
8 METHOD REFERENCE TABLE

class Arrays (java.util)

MethodPurpose
sort(arr)sorts in ascending order
toString(arr)readable String like [1, 2, 3]
binarySearch(arr, key)index of key in a sorted array
fill(arr, value)sets every element to value
equals(a1, a2)true if same length & elements
copyOf(arr, n)new array of length n, copied from arr

class ArrayList<T>

MethodPurpose
add(item)appends to end
add(i, item)inserts at index i
get(i)element at index i
set(i, item)replaces element at index i
remove(i) / remove(item)removes by index / by value
size()current number of elements
contains(item)true if present
indexOf(item)first index of item, or -1
clear()removes all elements