Saturday, March 13, 2010

Array

  • http://www.janeg.ca/scjp/lang/arrays.html
  • arrays are Java objects
  • all Java arrays are technically one-dimensional. Two-dimensional arrays are arrays of arrays.
  • declaring an array does not create an array object or allocate space in memory; it creates a variable with a reference to an array
  • array variable declarations must indicate a dimension by using []
    Examples of valid array declarations: (JJ pg84)      String[]s;     String []s;     String [] s;     String [ ] s;       // extra white space ignored     String[] s;     String[   ] s;      // extra white space ignored     String s[];     String s [];     String s [   ];     // extra white space ignored              String[] s[];     String[][]s;     String s [] [  ];   // extra white space ignored 
  • declaring the size of the array with the following notation is illegal
        String[5] s;        // illegal declaration 
  • the standard convention for declaring arrays is:
        String[] s;         // one-dimensional array     String[][] s;       // two-dimensional array 

Initializing arrays

  • all arrays are zero-based
  • arrays must be indexed by int values or byte, short or char values (as these can be promoted to int) (JLS §10.4)
  • using a long index value to access an array causes a compile error
  • attempting to access an array with an index less than 0 or greater than the length of the array causes anArrayIndexOutOfBoundsException to be thrown at runtime (JLS §10.4)
  • since arrays are Objects they can be initialized using the new operator
  • when created, arrays are automatically initialized with the default value of their type
    String[] s = new String[100];   // default values: null boolean[] b = new boolean[4];   // default values: false int[] i = new int[10][10];      // default values: 0 
  • array references declared as members are initialized to null BUT array references declared in methods are not initialized
class TestArray  {     int[] arr;          // member declaration, initialized to 'null'      public static void main(String[] args) {         int[] arr1;     // reference variable 'arr1' not initialized                  // compiles ok         System.out.println("arr:" + new TestArray().arr);          // compile error         System.out.println("arr1: " + arr1);     } } 
  • as arrays are allocated at runtime, you can use a variable to set their dimension
        int arrSize = 100;     String[] myArray = new String[arrSize]; 
  • you can use curly braces {} as part of an array declaration to initialize the array
        String[] oneDimArray = { "abc","def","xyz" }; 
Note
  • Curly braces {} can only be used in array declaration statements.
    String[] s;     // illegal initialization     s = { "abc", "def", "hij");               int[] arr = new int[] {1,2,3};  // legal 
  • you can assign an array a null value but you can't create an empty array by using a blank index
           int[] array = null;             // legal     // illegal initialization     int[] array = new int[];         

Initializing two-dimensional arrays

  • the first dimension represents the rows, the second dimension, the columns
  • curly braces {} may also be used to initialize two dimensional arrays. Again they are only valid in array declaration statements.
        int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} }; 
  • you can initialize the row dimension without initializing the columns but not vice versa
        int[][] myArray = new int[5][];     // illegal     int[][] myArray = new int[][5];  
  • the length of the columns can vary
      class TestTwoDimArrays {     // initialize # of rows     static int [][] myArray = new int[3][];       public static void main(String[] args) {      myArray[0] = new int[3];   // initialize # of cols     myArray[1] = new int[4];   // in each row     myArray[2] = new int[5];      for(int i=0; i<3; i="0;" i="0;">     

Also see

Sun Tech Tip: Manipulating Java Arrays

No comments:

Post a Comment