Liang Chapter 7: Array Basics, Processing, Copying, Passing, and 2D Arrays
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)
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)
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.
int[] a = new int[20];?boolean[] flags = new boolean[5];?Common array operations include initializing, printing, summing, finding max/min, and random shuffling. (Liang, Section 7.2.6)
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)
int[] b = a; and b[0] = 100;, what is a[0]?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)
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)
A method can return an array. The return type is the array type. (Liang, Section 7.6)
A two-dimensional array is an array of arrays. It can be used to represent a matrix or a table. (Liang, Section 7.8)
Java allows rows to have different lengths. This is called a ragged array. (Liang, Section 7.8.3)
matrix.length return for int[][] matrix = new int[3][4];?