Array
Array
Programming
Arrays
Motivation
Examples:
80-element array with base type char:
char[] symbol = new char[80];
pressure[3] = keyboard.nextInt();
System.out.println("You entered" + pressure[3]);
Some Array Terminology
Array name
temperature[n + 2]Index - also called a subscript
- must be an int,
- or an expression that evaluates to an int
temperature[n + 2]
Indexed variable - also called an
element or subscripted variable
temperature[n + 2] = 32;
Note that "element" may refer to either a single indexed
variable in the array or the value of a single indexed variable.
Array Length
Specified by the number in brackets when created with
new
maximum number of elements the array can hold
storage is allocated whether or not the elements are
assigned values
Subscript: 0 1 2 3
Value: 97 86 92 71
Subscript out of Range Error
Programming Tip:
Do not count on default initial values for array elements
explicitly initialize elements in the declaration or in a loop
Arrays, Classes, and Methods
An array of a class can This excerpt from the Sales Report program
be declared and the in the text uses the SalesAssociate class
class's methods applied
to the elements of the to create an array of sales associates:
array.
public void getFigures()
create an array of {
SalesAssociates System.out.println("Enter number of sales associates:");
numberOfAssociates = SavitchIn.readLineInt();
SalesAssociate[] record =
each array element is new SalesAssociate[numberOfAssociates];
a SalesAssociate for (int i = 0; i < numberOfAssociates; i++)
variable {
record[i] = new SalesAssociate();
System.out.println("Enter data for associate " + (i + 1));
use the readInput record[i].readInput();
method of System.out.println();
SalesAssociate }
}
Arrays and Array Elements
as Method Arguments
Arrays and array elements can be
used with classes and methods just like other objects
be an argument in a method
returned by methods
public static void main(String[] arg)
{
Indexed Scanner keyboard = new Scanner(System.in);a
Variables System.out.println("Enter your score on exam 1:");
int firstScore = keyboard.nextInt();
as Method int[ ] nextScore = new int[3];
int i;
Arguments double possibleAverage;
for (i = 0; i < nextScore.length; i++)
nextScore[i] = 80 + 10*i;
nextScore is for (i = 0; i < nextScore.length; i++)
an array of ints {
possibleAverage = average(firstScore,
nextScore[i]);
System.out.println("If your score on exam 2 is "
an element of
+ nextScore[i]);
nextScore is System.out.println("your average will be "
an argument of + possibleAverage);
method }
average }
public static double average(int n1, int n2)
average {
return (n1 + n2)/2.0; Excerpt from ArgumentDemo
method definition } program in text.
In this example, the output from the command line above will
be:
Hello Josephine Student
Using = with Array Names:
Remember They Are Reference Types
The output for this code will be " a does not equal b"
because the addresses of the arrays are not equal.
Behavior of Three Operations
method that }
}
returns true if and return match;
only the arrays }
an Array c = vowels();
for(int i = 0; i < c.length; i++)
System.out.println(c[i]);
}
public static char[] vowels()
the address {
of the array is char[] newArray = new char[5];
newArray[0] = 'a';
passed newArray[1] = 'e';
The local newArray[2] = 'i';
newArray[3] = 'o';
array name newArray[4] = 'u';
within the return newArray;
method is just }
}
c, newArray, and
another the return type of
name for the vowels are
original array
all the same type:
char []
Partially Filled Arrays
Sometimes only part of an array has been filled with data
Arrays with more than two dimensions are a simple extension of two-dimensional
(2-D) arrays
Indexes 0 1 2 3 4 5
0 $1050 $1055 $1060 $1065 $1070 $1075
1 $1103 $1113 $1124 $1134 $1145 $1156
2 $1158 $1174 $1191 $1208 $1225 $1242
3 $1216 $1239 $1262 $1286 $1311 $1335
Row Index 3 4 $1276 $1307 $1338 $1370 $1403 $1436
(4th row) … … … … … … …
Example: create a 2-D int array named b with 5 elements in the first row, 7 in
the second row, and 4 in the third row:
int[][] b = new int[3][];
b[0] = new int[5];
b[1] = new int[7];
b[2] = new int[4];
Programming Example:
Employee Time Records