CS2050: Arrays and Nodes

Arrays Basics

  • Primitive variables hold one value; arrays hold collections of like values, indexed from 00.

  • An array is an object, requiring a reference and new keyword for creation: int[] numbers = new int[6];.

  • Array elements are initialized to 00 for numeric types, false for booleans, null for objects.

  • Array size is fixed after creation; it cannot be changed.

  • Access elements using reference name and a subscript (index): numbers[0] = 20;.

  • Array indexes range from 00 to array.length - 1.

  • Off-by-one errors can lead to ArrayIndexOutOfBoundsException.

Array Initialization & Declaration

  • Initialization list for direct assignment: int[] days = {31, 28, 31};.

  • Alternate declaration style: int numbers[]; (less common).

Processing Arrays

  • Elements behave like regular variables in expressions and relational operations.

  • array.length field provides array size (constant, no parentheses).

  • Enhanced for loop for read-only processing: for (datatype elementVariable : array).

  • Arrays can be partially filled; use a counting variable to track valid data.

Array Copying & Comparison

  • Assigning one array reference to another (array2 = array1;) does not copy the array; both references point to the same object.

  • To copy, iterate and assign elements individually: secondArray[i] = firstArray[i];.

  • The == operator compares references, not contents. Compare contents element-by-element.

Array Operations

  • Finding highest/lowest value: Iterate through elements, comparing to a stored highest or lowest value.

  • Summing elements: Use an accumulator variable in a loop: total += units[i];.

  • Averaging elements: Sum elements then divide by array.length.

  • Arrays and Files: Use PrintWriter to write to a file, Scanner to read from a file into an array.

Arrays and Methods

  • Array elements or entire arrays can be passed as method arguments.

  • Passing an array passes its reference; methods can modify the original array.

  • Methods can return an array reference; the return type must be an array type: public static double[] getArray().

String Arrays

  • An array of String objects holds references to String objects.

  • If not initialized immediately, elements are null initially.

  • Each element is a String object, allowing String methods like toUpperCase(), length() (method, with parentheses), charAt().

  • Contrast array.length (field) with stringElement.length() (method).

Arrays of Objects

  • Arrays can store objects of any class (InventoryItem[] inventory = new InventoryItem[5];).

  • Each element (object reference) must be individually initialized with new for the object itself.

Two-Dimensional Arrays

  • Arrays of arrays, visualized as rows and columns: double[][] scores = new double[3][4];.

  • Accessed with two subscripts: scores[row][col].

  • Processed using nested loops.

  • Initialized with nested braces: {{1, 2}, {3, 4}}.

  • numbers.length gives the number of rows.

  • numbers[row].length gives the number of columns in that specific row.

  • Ragged arrays have rows of different lengths: int[][] ragged = new int[4][]; then ragged[0] = new int[3];.

More Dimensions

  • Arrays can have three or more dimensions, though visualization becomes complex.

Command-Line Arguments

  • The main method receives command-line arguments as a String[] args parameter.

  • Each word on the command line becomes an element in the args array.