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 valuesfor the type of the array.Access an elementof an 1D array using an array name and an index.nums[0]Traversean array ofprimitive values.Traversean array ofobject references.Valid indexof an 1D array:0 to length-1.- Pass an
array asanargumentinto a method.- A method can
return an arrayas a result.- The
lengthattribute of an array is final, itcan not be changed.Note: how the index changes (i++, i+=2, i--)
2. Knowledge Points
(1) Understand and Create Arrays
- An
arraystoresmultiple valuesof thesame type. - The values can be either
primitive valuesorobject 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
lengthof an array is established at the time of creation andcannot be changed. (fixed length) - The length of an array can be accessed through the length attribute.
- Example:
numbers.length
- Example:
- When an array is created using the keyword new, all of its elements are initialized to the
default valuesfor the element data type.- The
default valueis:- int : 0
- double : 0.0
- boolean : false
- reference type (ex. String, Student, Dog) : null
- The
- Two ways to create an array:
- using the
newkeyword: Ex.int[] numbers= new int[5]; - using an
initializer list: Ex.int[] nums = { 11, 22, 33, 44, 55 };
- using the
(2) Access Array Elements
- Access an element in one 1D array:
arrayName[index].- Example:
numbers[0]
- Example:
- The
valid indexvalues for an array are0 to length-1. - Using an
indexvalueoutside of this rangewill result in anArrayIndexOutOfBoundsException.
(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); } - Example:
- 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); } - Example:
(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 + " "); }
- Example:
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()); }
- Example: