CS2050: Arrays and Nodes
Arrays Basics
Primitive variables hold one value; arrays hold collections of like values, indexed from .
An array is an object, requiring a reference and
newkeyword for creation:int[] numbers = new int[6];.Array elements are initialized to for numeric types,
falsefor booleans,nullfor 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 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.lengthfield 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
highestorlowestvalue.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
PrintWriterto write to a file,Scannerto 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
Stringobjects holds references toStringobjects.If not initialized immediately, elements are
nullinitially.Each element is a
Stringobject, allowingStringmethods liketoUpperCase(),length()(method, with parentheses),charAt().Contrast
array.length(field) withstringElement.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
newfor 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.lengthgives the number of rows.numbers[row].lengthgives the number of columns in that specific row.Ragged arrays have rows of different lengths:
int[][] ragged = new int[4][];thenragged[0] = new int[3];.
More Dimensions
Arrays can have three or more dimensions, though visualization becomes complex.
Command-Line Arguments
The
mainmethod receives command-line arguments as aString[] argsparameter.Each word on the command line becomes an element in the
argsarray.