參考 p.222 程式 Listing 6 AnalyzeNumbers.java
an array as a collection of variables of the same type (Object 時可以有不同,同一繼承(家族)即可)
elementType[] arrayRefVar; double[] myList; // C 使用方式,也可以,但不建議使用 double myList[];
arrayRefVar = new elementType[arraySize]; elementType arrayRefVar = new elementType[arraySize]; double[] myList = new double[10];
p.223 assign values to the elements
arrayRefVar[index] = value; double[] myList = new double[10]; myList[0] = 5.6; myList[1] = 4.5; // ... myList[9] = 11123;
p.224 Figure 6.1 The array myList has ten elements of double type and int indices from 0 to 9.
p.224 Note: An array variable contains a reference to that array
When space for an array is allocated, the array size must be given
// arrayRefVar.length double[] myList = new double[10]; int len = myList.length // 10, index from 0 to 9 // first myList[0] = 5.6; // last myList[9] = 11123;
array indices are 0 based; that is, they range from 0 to arrayRefVar.length-1
myList[2] = myList[0] + myList[1]; for (int i = 0; i < myList.length; i++) { myList[i] = i; }
elementType[] arrayRefVar = {value0, value1, ..., valuek}; double[] myList = {1.9, 2.9, 3.4, 3.5};
double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;
// 沒有 new double[] myList = {1.9, 2.9, 3.4, 3.5};
p.225 Caution
// 分開為2個敘述,錯誤 double[] myList; myList = {1.9, 2.9, 3.4, 3.5};
通常使用 for 處理陣列(Array)
for (elementType element: arrayRefVar) { // Process the element }
for (double u: myList) { System.out.println(u); }
參考 p.229 程式 Listing 6.1 LottoNumbers.java
Input/Output Redirection 輸入/輸出 重新導向 (I/O Redirection)
java LottoNumbers < LottoNumbers.txt
參考 p.231 程式 Listing 6.2 DeckOfCards.java
list2 = list1;
p.232 Figure 6.4 Before and After list2 = list1;
int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArray.length; i++) { targetArray[i] = sourceArray[i]; }
arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
public static void showCard(int[] card) { for(int k = 0; k<card.length; k++ ) { String suit = csuits[card[k] / 13]; String rank = ranks[card[k] % 13]; System.out.printf("%s %s ", suit, rank); } System.out.println(); }
showCard(playerA); showCard(new int[] {3, 1, 2, 6, 4, 2});
Java uses pass-by-value to pass arguments to a method, passing the values of variables of primitive data types and passing arrays
int x = 1; // x represents an int value int[] y = new int[10]; // y represents an array of int values ; // Invoke m with arguments x and y m(x, y); System.out.println("x is " + x); System.out.println("y[0] is " + y[0]); public static void m(int num, int[] numbers) { num = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0] }
p.234 Figure 6.5 The primitive type value in x is passed to number, and the reference value in y is passed to numbers.
參考 p.235 程式 Listing 6.3 TestPassArray.java
p.236 Figure 6.6 When passing an array to a method, the reference of the array is passed to the method.
public static int[] reverse(int[] list) { int[] result = new int[list.length]; for (int k = 0, n = list.length - 1; k < list.length; k++, n--) { result[n] = list[k]; } return result; }
參考 p.237 程式 Listing 6.4 CountLettersInArray.java
p.239 Figure 6.8 (a) An array of 100 characters is created when executing createArray. (b) This array is returned and assigned to the variable chars in the main method.
typeName... parameterName // 注意 ...
參考 p.239 程式 Listing 6.5 VarArgsDemo.java
Searching is the process of looking for a specific element in an array, two commonly used approaches, linear search and binary search.
參考 p.240 程式 Listing 6.6 LinearSearch.java
參考 p.2 程式 Listing 6. BinarySearch.java
參考 p.244 程式 Listing 6.8 SelectionSort.java
p.245 Figure 6.11 Selection sort repeatedly selects the smallest number and swaps it with the first number in the list.
參考 p.2 程式 Listing 6. InsertionSort.java
p.245 Figure 6.12 Insertion sort repeatedly inserts a new element into a sorted sublist.
double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; java.util.Arrays.sort(numbers); // Sort the whole array char[] chars = {'a', 'A', '4', 'F', 'D', 'P'}; java.util.Arrays.sort(chars, 1, 3); // Sort part of the array
int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79}; System.out.println("(2) Index is " + java.util.Arrays.binarySearch(list, 11)); System.out.println("(1) Index is " + java.util.Arrays.binarySearch(list, 12)); char[] chars = {'a', 'c', 'g', 'x', 'y', 'z'}; System.out.println("(3) Index is " + java.util.Arrays.binarySearch(chars, 'a')); System.out.println("(4) Index is " + java.util.Arrays.binarySearch(chars, 't'));