Array Code Assignment: Array of Arrays
minor  revision May 10/ 2001                                                                P.Komisar


class Seasons{

public static void main(String[] args){

String[][] seasons = new String[4][ ];
// main array size is set but subarrays can vary in size

String[] Winter=new String[]{"Jan","Feb","Mar"};
String[] Spring=new String[]{"Apr","May","June"};
String[] Summer=new String[]{"July","Aug","Sept"};
String[] Fall=new String[]{"Oct","Nov","Dec"};

seasons[0]=Winter;
seasons[1]=Spring;
seasons[2]=Summer;
seasons[3]=Fall;

for(int i=0;i<seasons.length;i++){
for(int j=0;j<3;j++){
System.out.println(seasons[i][j]);
      }
    }
  }
}

1.     Compile and run the above class. Then, adapt the above class to create a new 'array
        of arrays' whose values will be printed to screen. Your class will contain three fictitious
        warehouses, named by a different country where each might exist. For each warehouse
        create subarrays containing string literals describing the five most important items stored
        at the warehouse. Write compile and run the new class so that these values are printed
        to screen.



 

class arrayCopy{
public static void main(String[] args){           //********* Code Snippet 1

   String[]  statement = new String[] { "It's", "not", "true", "it", "was", "my", "plan"};
   String[] out_of_context= new String[ 4];
   System.arraycopy(statement, 3, out_of_context, 0, 4);
      for(int i=0; i <out_of_context.length; i++)
        System.out.print(out_of_context[i]+" ");
        System.out.println("\n");
 //******************************************Code Snippet 2  // shift operators
int shift = 1000;
System.out.println(shift);    //before

     shift = 1000 >> 2;
System.out.println(shift);    // after
  }
}


Questions    // continued

Q2. Rewrite arrayCopy so that it writes NOT TRUE.
Q3  Substitute -1000 in for 1000 above. Report what you observe?
Q4. Keeping -1000 in place, change the operator to the unsigned right shift operator, >>>.
       Explain what happens? 

//  Reminder: The unsigned right shift operator( >>> ) performs what is called a logical shift in filling
//   the most significant bits with zeros. The signed right shift operator ( >> )does an arithmetic shift
//  (keeping  the sign, in this case, by filling with ones).