Chapter 18
Array Objects

   18.1 One-Dimensional Arrays
   18.2 Two-Dimensional Arrays
   18.3 Distinguished Arrays
   18.4 Assignment #23: Arrays

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] +  " " ); 
} 
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

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; 
} } 
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

Exercises

18.4 Assignment #23: Arrays

Due: Fr, June 6, midnight

  1. 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.

    1. The constructor should assign to ‘arrayn entries initialized to 0.
    2. The ‘get’ method should return a pointer to the array object.
    3. The ‘max’ method should return the largest value stored in the array.
    4. 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;
       }
     }
  2. 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