Chapter 18
Array Objects
An array is a collection of memory cells of identical types. The cells are accessed through a common reference, and
are distinguished by integer indexes. Java embeds arrays within objects of implicit classes of the core language.
Besides for the fields assigned for the arrays, the objects are also assigned fields that hold the dimensions of the
arrays.
18.1 One-Dimensional Arrays
int [] a = {1,2};
int [] b = new int[a.length];
for( i=0; i<a.length; i=i+1 ){
b[i] = a[i];
}
for( i=0; i<b.length; i=i+1 ){
System.out.println( b[i] + " " );
}
- An array variable holds a pointer to the storage of the array values.
- Pointers may be copied through the assignment operator ‘=’.
int [] a = {1,2};
int [] b = a;
a[0] = 3;
for( i=0; i<b.length; i=i+1 ){
System.out.println( b[i] + " " );
}
18.2 Two-Dimensional Arrays
- Declaration
<type> [][] <name>
<type> <name> [][]
- Memory allocation
new <type> [<row's limit>] [<col's limit>]
- Data-driven memory allocation
{
{ <colon-separated list of values> },
..... ,
{ <colon-separated list of values> }
}
- References with double indexes.
<name> [ <index> ] [ <index> ]
int [][] a = new int [2][3];
for( i=0; i<2; i=i+1 ){
for( j=0; j<3; j=j+1 ){
a[i][j] = i * j;
} }
- The rows may be accessed as one-dimensional arrays
- The rows might differ in length
int [][] a = { {1,2,3},
{4,5} };
for( i=0; i<a.length; i=i+1 ){
System.out.println( a[i].length + " " );
}
18.3 Distinguished Arrays
- The ‘main’ method has a String array for a parameter
- The parameter of the ‘main’ method gets its arguments from the command line.
Exercises
18.4 Assignment #23: Arrays
Due: Fr, June 6, midnight
- Provide to the given ‘coinArray.java’ program the missing part of the ‘Array’ class. The class should have the
interface
class Array {
int [] array;
Array(int n);
boolean full();
int [] get();
void increment(int i);
int max();
}
and satisfy the following conditions.
- The constructor should assign to ‘array’ n entries initialized to 0.
- The ‘get’ method should return a pointer to the array object.
- The ‘max’ method should return the largest value stored in the array.
- The ‘increment’ method should increase by one the value in the i’th entry.
coinArray.java
class coinArray{
public static void main(String [] args) {
int flips=10;
Array graph = new Array( flips+1 );
int i, heads, tails;
while( !graph.full() ){
heads = 0; tails=0;
for(i=0; i<flips; i++ ){
if( Math.random() < 0.5 ){ heads++; }
else { tails++; }
}
graph.increment( heads );
}
int [] a = graph.get();
for(i=0; i<a.length; i++ ){
System.out.print( a[i] + " ");
}
System.out.println();
}
}
class Array{
boolean full(){
double bound, n=array.length;
bound = (n>13? 3.4 : 2)* Math.max(n,10) - n;
return max() > bound;
}
}
- Add the ‘Array’ class to the coin.java program of the given web page, then compile and execute the program. (The program illustrates the binomial distribution obtained from flipping
coins.)
Assume ‘lab23’ for the submit program, and submit the file ‘coin.java’.
Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade
of 0 points.
Q&A