bembry.org
Home / Technology / Java

Java Lesson 5: Arrays

  • Creating Arrays
    • When creating an array, you must declare the array variable then define a new array assigned to the variable.
    • Define the array variable using the following syntax:
      int[] myArray;
    • Define the new array, including its size, and assign it to the existing variable using the following syntax:
      myArray = new int[10];
    • You may define both the variable and the array in a single line
      int[] myArray = new int[10];
    • You may also define the variable and populate the array in one line
      int[] myArray = {2, 5, 7, 31, 32}
    • Note that the array variable is distinct from the array itself.
    • Every item in the array must be of the same data type. You cannot have an array that has a mix of int and char or float values.
    • If an array is defined with a fixed size (instead of an implicit size garnered from a set of values), all values in the array are preset to a default value (such as 0 if it is an array of type int, or 0.0 for an array of floats)
    • Arrays are fixed sizes. Once you declare the size of an array, you cannot change it.
  • Multi-dimensional Arrays
    • Arrays can be nested to create multidimensional arrays.
      int[][] myArray = new int [20][10]; A 2-dimensional array
      int[][][] myArray = new int [20][10][]; A 3-D array
    • Note that both the variable declaration and the array definition must have the same number of [] pairs.
    • The sizes of the deeper arrays may be left undefined, allowing each nested array to have a different size.
  • Working with Arrays
    • Access array items by their index number. Note that arrays start with the index number 0 and use integers as index numbers.
              int[] myArray = {2, 5, 7, 31, 32};
              myAge = myArray[0];
              
    • You may also reassign array values by using their index number
              int[] myArray = {2, 5, 7, 31, 32};
              myArray[3] = 33;
              
    • Use the length attribute to find the size of an array
              int[] myArray = {2, 5, 7, 31, 32};
              myArraySize = myArray.length;
              
    • To access values in deeper arrays, use the following syntax
      myData = myArray[3][5];
  • Example
    public class MyArrays
    {
        public static void main(String[] args)
        {
            // Create then populate an array
            int[] numbers = new int[5];
            numbers[0] = 2;
            numbers[1] = 4;
            numbers[2] = 6;
            numbers[3] = 8;
            numbers[4] = 10;
    
            // Create and populate in one line a nested array (2-D)
           char[][] letters = {{'A','a'}, {'B','b'}, {'C','c'}, {'D','d'}, {'E','e'}};
    
            // Ouput a value from each array
            for (int i = 0; i <=4; i++)
            {
                int capOrLower = (int)(2*Math.random()); // random 0 or 1
                System.out.println(numbers[i] +"-"+ letters[i][capOrLower]);
            }
        }
    }
                
    
Restricted access