Home Basics Processing Copying Passing 2D Arrays

Week 2: Arrays (Part 1)

Liang Chapter 7: Array Basics, Processing, Copying, Passing, and 2D Arrays

Array Basics

An array is a data structure that stores a collection of values of the same type. Once created, its size is fixed. (Liang, Section 7.2)

Declaring and Creating Arrays

// Declaring and creating double[] myList = new double[10]; // Array initializer int[] numbers = {1, 2, 3, 4, 5};

Default Values

When an array is created with new, all elements are initialized to default values: 0 for numeric types, false for boolean, '\u0000' for char, and null for object types. (Liang, Section 7.2.2)

Accessing Elements

Array elements are accessed through the index. The index starts from 0 and goes to array.length - 1. Accessing an index outside this range throws ArrayIndexOutOfBoundsException.

myList[0] = 5.6; // Set first element myList[9] = 3.4; // Set last element System.out.println(myList.length); // 10
Q1: What is the index of the last element in int[] a = new int[20];?
A) 20
B) 19
C) 0
D) 21
Arrays are 0-indexed. An array of size 20 has indices 0 through 19. (Liang, Section 7.2.3)
Q2: What is the default value of elements in boolean[] flags = new boolean[5];?
A) false
B) true
C) 0
D) null
The default value for boolean arrays is false. (Liang, Section 7.2.2)

Section Score

0 / 0

Processing Arrays

Common array operations include initializing, printing, summing, finding max/min, and random shuffling. (Liang, Section 7.2.6)

Summing and Finding Max

double[] myList = {1.9, 2.9, 3.4, 3.5}; // Sum all elements double total = 0; for (int i = 0; i < myList.length; i++) total += myList[i]; // Find the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) if (myList[i] > max) max = myList[i];

Random Shuffling

for (int i = myList.length - 1; i > 0; i--) { int j = (int)(Math.random() * (i + 1)); // Swap myList[i] with myList[j] double temp = myList[i]; myList[i] = myList[j]; myList[j] = temp; }

The for-each Loop

for (double e : myList) { System.out.println(e); }

Trace the Code

int[] a = {2, 4, 6, 8}; int sum = 0; for (int i = 0; i < a.length; i += 2) sum += a[i]; System.out.println(sum);

Section Score

0 / 0

Copying Arrays

Assigning one array variable to another does not copy the array contents; it copies the reference. To copy contents, use a loop, System.arraycopy, or Arrays.copyOf. (Liang, Section 7.4)

Assignment vs. Copying

int[] source = {1, 2, 3}; int[] target = source; // Both reference the SAME array! target[0] = 99; System.out.println(source[0]); // 99 (not 1!)

Proper Copying Methods

// Method 1: Loop int[] copy1 = new int[source.length]; for (int i = 0; i < source.length; i++) copy1[i] = source[i]; // Method 2: System.arraycopy int[] copy2 = new int[source.length]; System.arraycopy(source, 0, copy2, 0, source.length); // Method 3: Arrays.copyOf int[] copy3 = java.util.Arrays.copyOf(source, source.length);
Q1: After int[] b = a; and b[0] = 100;, what is a[0]?
A) 100
B) The original value
C) 0
D) Compilation error
Array assignment copies the reference, not contents. Both a and b point to the same array. (Liang, Section 7.4)

Section Score

0 / 0

Passing Arrays to Methods

When you pass an array to a method, the reference of the array is passed. The method can modify the array elements. (Liang, Section 7.5)

public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println(); } public static void main(String[] args) { int[] numbers = {3, 1, 2, 6, 4}; printArray(numbers); // Pass array to method }

Pass by Value (of reference)

Java is always pass-by-value. For arrays, the value passed is the reference (memory address). This means the method receives a copy of the reference, but both reference the same array object. Changes to elements inside the method affect the original array. (Liang, Section 7.5)

Trace the Code

public static void modify(int[] arr) { arr[0] = 99; arr = new int[]{-1, -2}; } int[] x = {1, 2, 3}; modify(x); System.out.println(x[0] + " " + x.length);

Section Score

0 / 0

Returning Arrays from Methods

A method can return an array. The return type is the array type. (Liang, Section 7.6)

public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int i = 0; i < list.length; i++) result[i] = list[list.length - 1 - i]; return result; } int[] original = {1, 2, 3, 4}; int[] reversed = reverse(original); // reversed is {4, 3, 2, 1}
Q1: What is the return type of a method that returns an integer array?
A) int
B) int[]
C) Array
D) void
To return an array, the return type must include the brackets: int[]. (Liang, Section 7.6)

Section Score

0 / 0

Two-Dimensional Arrays

A two-dimensional array is an array of arrays. It can be used to represent a matrix or a table. (Liang, Section 7.8)

Declaring and Creating 2D Arrays

// Declare and create int[][] matrix = new int[3][4]; // 3 rows, 4 columns // Initializer int[][] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

Processing a 2D Array

// Print all elements for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) System.out.print(grid[i][j] + " "); System.out.println(); }

Ragged Arrays

Java allows rows to have different lengths. This is called a ragged array. (Liang, Section 7.8.3)

int[][] ragged = new int[3][]; ragged[0] = new int[2]; // row 0 has 2 columns ragged[1] = new int[4]; // row 1 has 4 columns ragged[2] = new int[3]; // row 2 has 3 columns
Q1: What does matrix.length return for int[][] matrix = new int[3][4];?
A) 3 (number of rows)
B) 4 (number of columns)
C) 12 (total elements)
D) 7
matrix.length gives the number of rows (3). matrix[0].length gives the number of columns in row 0 (4). (Liang, Section 7.8)

Trace the Code

int[][] m = {{1,2},{3,4},{5,6}}; int sum = 0; for (int i = 0; i < m.length; i++) sum += m[i][0]; System.out.println(sum);

Find the Bug

int[][] matrix = new int[3][3]; for (int i = 0; i <= matrix.length; i++) for (int j = 0; j < matrix[i].length; j++) matrix[i][j] = i + j;
What is the bug?
A) i <= matrix.length should be i < matrix.length (off-by-one, causes ArrayIndexOutOfBoundsException)
B) Missing braces
C) Wrong initial value of i
The condition i <= matrix.length causes i to reach 3, which is out of bounds. Use i < matrix.length. (Liang, Section 7.8)

Section Score

0 / 0

Week 2

Array Basics Processing Copying Passing Returning 2D Arrays