ch6 Single-Dimensional Arrays

6-1 Introduction

參考 p.222 程式 Listing 6 AnalyzeNumbers.java

6-2 Array Basics

an array as a collection of variables of the same type (Object 時可以有不同,同一繼承(家族)即可)

6-2-1 Declaring Array Variables

elementType[] arrayRefVar;

double[] myList;
// C 使用方式,也可以,但不建議使用
double myList[];

6-2-2 Creating Arrays

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

6-2-3 Array Size and Default Values

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

6-2-4 Array Indexed Variables

myList[2] = myList[0] + myList[1];

for (int i = 0; i < myList.length; i++) {
	myList[i] = i;
}

6-2-5 Array Initializers

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};

6-2-6 Processing Arrays

通常使用 for 處理陣列(Array)

  1. Initializing arrays with input values 輸入陣列元素初值
  2. Initializing arrays with random values 設定陣列元素為隨機值
  3. Displaying arrays 顯示陣列元素
  4. Summing all elements 將陣列所有元素加總
  5. Finding the largest element 找出陣列中最大值
  6. Finding the smallest index of the largest element 找出陣列中最大值的最小索引(index)
  7. Random shuffling 洗牌 
  8. Shifting elements 將陣列元素左移或右移

6-2-7 For-each Loops

for (elementType element: arrayRefVar) {
	// Process the element
}
for (double u: myList) {
	System.out.println(u);
}

6-3 Problem: Lotto Numbers

參考 p.229 程式 Listing 6.1 LottoNumbers.java

Input/Output Redirection 輸入/輸出 重新導向 (I/O Redirection)

java LottoNumbers < LottoNumbers.txt

6-4 Problem: Deck of Cards

參考 p.231 程式 Listing 6.2 DeckOfCards.java

6-5 Copying Arrays

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);

6-6 Passing Arrays to Methods

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.

6-6-1 Passing Array Arguments

參考 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.

6-7 Return an Array from a 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;
}

6-7-1 Case Study: Counting the Occurrences of Each Letter

參考 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.

6-8 Variable-Length Argument Lists

typeName... parameterName  // 注意 ...

參考 p.239 程式 Listing 6.5 VarArgsDemo.java

6-9 Searching Arrays

Searching is the process of looking for a specific element in an array, two commonly used approaches, linear search and binary search.

6-9-1 The Linear Search Approach

參考 p.240 程式 Listing 6.6 LinearSearch.java

6-9-2 The Binary Search Approach

參考 p.2 程式 Listing 6. BinarySearch.java

6-10 Sorting Arrays

6-10-1 Selection Sort

參考 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.

6-10-2 Insertion Sort

參考 p.2 程式 Listing 6. InsertionSort.java

p.245 Figure 6.12 Insertion sort repeatedly inserts a new element into a sorted sublist.

6-11 The Arrays Class

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'));

參考 p.2 程式 Listing 6. Ran.java

p.2 Figure 6. Black