Arrays

by Michael Thomas

(Tutorial Home Page)

by Michael Thomas

(Tutorial Home Page)

Objectives (topics)

Topic Notes
Arrays Arrays
  • Arrays are objects.
  • Java initializes each element in an array to its default value when the array is created.  Object arrays are initialized with "null"
  • The first element in an array is element (index) 0.  The last element is the length - 1.
  • Can use {} to initialize an array only when the array is declared.
  • [][] declares a nested array.  The number of brackets determines the level of array nesting.
  • In the declaration, [] may come before or after the array's name. 
    Ex:  int [] myArrayA; int myArrayB [];
    Ex:  int [] [] myArrayC; int myArrayD [] [];
  • Determine the length of any array with:  array_name.length;
    Ex:  int [] myArray = new int[5]; 
    Ex:  System.out.println("Length = "+ myArray.length);  //shows Length = 5.
  • Once an array is created with the keyword "new" the array can not be resized.
Array, declaration Declaration Examples
  • int [] myIntArray;  //Array of int's.
  • MyObjects [] MyObjectArray;  //Array of objects
  • int [] myIntArray = new int[5];  //Initialize & create an array of integers with 5 Rows
  • int myInt = 5; int [] myIntArray = new int[myInt];  //5 Rows
  • int k[] = new int[] {0,1,2,3,4};
  • int k[] = {0,1,2,3,4};
  • String [] myString = {"Hello", "Michael"};  // 2 rows
  • String [] [] myString = { {"1","2"}, {"10","11"} }; // 2 by 2

Compile Errors:

  • int k[]= new int[5] {0,1,2,3,4};  // see [5], error with {}.
Array declaration & initialization Declaration of a Array - to declare that an array will contain a certain data type.  The array does not exist in memory yet.

Initialization of an Array - to create an array object in memory.  If data is passed via the {} identifier then the array is populated with the elements passed, else the array is populated with a default value.  Default value for Data Types that extend Object is the null value.  Defaults for primative integers is 0, floating point is 0.00, boolean is false, and char is \u0000 .

Declaration only:  int k [];
Initialization only:   k = new int [3]
Initialization only:   k = new int [intSize]
Declaration & Initializationint k = {0,1,2}

Declaration must always come before initialization.

Remember this, you can not use the {} to load an array once the array is initialized.  That's why you can only use the {} in the initialization of the array.

Also, int k[]= new int[ 5 ] {0,1,2,3,4}; is a compile error because the array was initialized as of:  int k[]= new int[ 5 ]

Use the {} in the initialization of an array when you know what elements will reside in the array. 
Ex:  int k [] = {0,1,2,3,4}

Use the [] when you don't know how many elements the array will have at the time of declaration.  Ex:  int k [];   Later you can initialize the array with k = new int [intArraySize]

Use the [5] when you know how many elements the array will have at the time you declare and initialize the array in one statement.  Ex int k [] = new int [5]