4.3 Array Creation and Access

11 阅读2分钟

1. Exam Points

  • Create an array (length can be 0, but must be specified).
  • When an array is created, all of its elements are initialized to the default values for the type of the array.
  • Access an element of an 1D array using an array name and an index. nums[0]
  • Traverse an array of primitive values.
  • Traverse an array of object references.
  • Valid index of an 1D array: 0 to length-1.
  • Pass an array as an argument into a method.
  • A method can return an array as a result.
  • The length attribute of an array is final, it can not be changed.
  • Note: how the index changes (i++, i+=2, i--)

2. Knowledge Points

(1) Understand and Create Arrays

  • An array stores multiple values of the same type.
  • The values can be either primitive values or object references.
  • Examples:
    // Arrays of primitive values
    int[] numbers = new int[5];
    boolean[] flags = new boolean[6];
    double[] prices = new double[10];
          
    // Arrays of object references
    Dog[] dogs = new Dog[10];
    Student[] students = new Student[20];
    
  • The length of an array is established at the time of creation and cannot be changed. (fixed length)
  • The length of an array can be accessed through the length attribute.
    • Example: numbers.length
  • When an array is created using the keyword new, all of its elements are initialized to the default values for the element data type.
    • The default value is:
      • int : 0
      • double : 0.0
      • boolean : false
      • reference type (ex. String, Student, Dog) : null
  • Two ways to create an array:
    • using the new keyword: Ex. int[] numbers= new int[5];
    • using an initializer list: Ex. int[] nums = { 11, 22, 33, 44, 55 };

(2) Access Array Elements

  • Access an element in one 1D array: arrayName[index] .
    • Example: numbers[0]
  • The valid index values for an array are 0 to length-1.
  • Using an index value outside of this range will result in an ArrayIndexOutOfBoundsException.

(3) Arrays as Parameters or Return Types

  • An array can be passed into a method as an argument(实际参数). (arrays as parameters)
    • Example:
    // Here, the method takes a double array as a parameter(形式参数)
    static double sum(double[] array) {
        double sum = 0.0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return sum;
    }
        
    public static void main(String[] args) {
        double[] prices = {1.2, 3.5, 6.7, 9.0};
        // here, the array 'prices' is passed into the 'sum' method as an argument(实际参数).
        double result = sum(prices);
    }
    
  • An array can be returned as the result in a method. (arrays as return types)
    • Example:
    // here, this method takes a double array as a parameter, 
    // and returns another array as the result.
    static double[] changePrices(double[] array) {
        double[] newArray = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            newArray[i] = array[i] * 0.8;
        }
        return newArray;
    }
    public static void main(String[] args) {
        double[] prices = {1.2, 3.5, 6.7, 9.0};
        // here we use a variable of the 'double[]' type (same as the return type) 
        // to accept the returned result of the 'changePrices' method.
        double[] anotherArray = sum(prices);
    }
    

(4) Traverse 1D Arrays

  • Traverse an 1D array of primitive values.
    • Example:
      int[] nums = { 1, 2, 3, 4 };
      // 1. for loop (use indexes)
      for (int i = 0; i < nums.length; i++) { 
          // i is an index, nums[i] is an element
          System.out.print(nums[i] + " ");
      }
      
      System.out.println();
      
      // 2. for-each loop (no use of indexes)
      for (int x : nums) { // here type 'int' follows the type of the array
          // here x is the current element
          System.out.print(x + " ");
      }
      
  • Traverse an 1D array of object references.
    • Example:
      class Pig {
          
          private String name;
          private boolean vaccinated;
          
          public Pig(String n, boolean v) {
              name = n;
              vaccinated = v;
          }
          
          public String getName() {
              return name;
          }
          
          public boolean isVaccinated() {
              return vaccinated;
          }
      }
      
      // 1. use for loops (use indexes)
      for (int i = 0; i < pigs.length; i++) {
          System.out.println(pigs[i].getName() + "-" + pigs[i].isVaccinated());
      }
      
      // 2. use for-each loops (no use of indexes)
      for (Pig p : pigs) {
          System.out.println(p.getName() + "-" + p.isVaccinated());
      }
      

3. Exercises