Arrays

Terminology

Array
A hom0geneous collection of data referred to by a single name and a subscript.
In Java, an array is a reference type composed of elements of either primitive types or reference types.
Subscript (index)
The offset of an element of an array. The first element is subscript 0.
Element
A single member of the array collection.
ArrayIndexOutOfBoundsException
An exception (or run-time error) caused by trying to access an array element that does not exist.  For example trying to access myArray[6] when there re only 6 elements in the array.
Initializer List
A mechanism for instantiating an array using a list of values to initialize the elements.

Arrays
  1. What are they? Collection of "cells" or containers.
  2. How are they defined? Declaration, instantiation, initializer list.
  3. How are they reference? Subscripted notation - See ArrayDemo1.java (If this does not open for you, see the copy in Canvas).
  4. Loop processing - See ArrayDemo2.java
  5. Array processing errors are similar to loop processing. InvalidSubscript.java
  6. One solution - Use the .length attribute of arrays. InvalidSubscriptFixed.java
  7. Initializer lists. ArrayInitialization.java
  8. For each loop. ArrayInitializationForEach.java
  9. Inputting number of elements. DisplayTestScores.java
  10. Passing individual elements. PassElements.java

Things to remember about arrays:

Feature Syntax Example
Declaration data-type [] variable-name; int [] gradeArray;
Card [] deck;
Declaration
(alternative)
data-type variable-name[]; int gradeArray [];
Card deck [];
Initializer list data-type [] list = { value, value, ... value} String [] names = { "bob", "ann", "amy", "sue", "sam"};
Instantiation new data-type[int-value] intArray = new int [25];
deck = new Card [cardCount];
Instantiation and initialization new data-type[] { value, value, ... value } names = new String [] { "bob", "ann", "sue", "sam" }; return new int [] {0};
Referring to the
array as a whole
var-name return deck;
intArray = grades;

array2.equals(array1);
(What do you think this does compares?)

arrLength = names.length;

Specialized Arrays methods java.util.Arrays.fill(grades, 100);
java.util.Arrays.equals(section1, section2);
java.util.Arrays.sort(names);
java.util.Arrays.toString(names)
Referring to array elements var-name[int-value] return deck[0];
intArray[ii] = grades[jj];
rank = deck[ii].getRank();
while (intArray[ii] == 5)