Chapter 5 Arrays Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word hapter I Introduction to Computers, Programs, and java Chapter 2 Primitive Data Types and operations Chapter 3 Control Statements Chapter 4 Methods hapter 5 Arrays 最终也要试一试,千万别站在那里犹豫 Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 5 Arrays Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Control Statements Chapter 5 Arrays Chapter 4 Methods Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Prerequisites for Part I 最终也要试一试,千万别站在那里犹豫
Objectives o To describe why an array is necessary in programming($ 5.1) o To learn the steps involved in using arrays: declaring array reference variables and creating arrays( s 5.2) ● To initialize the values in an array(§52) o To simplify programming using JDK 1.5 enhanced for loop(s 5.2) ● To copy contents from one array to another(§53 To develop and invoke methods with array arguments and ruturn type(§5455) o To sort an array using the selection sort algorithm($ 5.6) To search elements using the linear or binary search algorithm o To declare and create multidimensional arrays($ 5.8) o To declare and create multidimensional arrays($5.9 Optional) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To describe why an array is necessary in programming (§5.1). ⚫ To learn the steps involved in using arrays: declaring array reference variables and creating arrays (§5.2). ⚫ To initialize the values in an array (§5.2). ⚫ To simplify programming using JDK 1.5 enhanced for loop (§5.2). ⚫ To copy contents from one array to another (§5.3). ⚫ To develop and invoke methods with array arguments and ruturn type (§5.4-5.5). ⚫ To sort an array using the selection sort algorithm (§5.6). ⚫ To search elements using the linear or binary search algorithm (§5.7). ⚫ To declare and create multidimensional arrays (§5.8). ⚫ To declare and create multidimensional arrays (§5.9 Optional)
Introducing arrays ● Array in logical O Data structures o Related data items of same type O Remain same size once created ● array in physical O Group of contiguous memory locations o Each memory location has same name o Each memory location has same type Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Introducing Arrays ⚫ Array in logical Data structures Related data items of same type Remain same size once created ⚫ Array in physical Group of contiguous memory locations ⚫Each memory location has same name ⚫Each memory location has same type
Name of array(note c[0 45 that all elements of c this array have the 6 same name c) c[2 0 c[3 72 1543 c 89 c[6 0 c[7 62 3 Position number(index c[9 1 of subscript)of the 6453 element within array c c[10] [11] 78 Fig. 7.1 A 12-element array Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 Fig. 7.1 A 12-element array. -45 6 0 72 1543 -89 0 62 -3 1 6453 78 c[ 1 ] c[ 2 ] c[ 4 ] c[ 3 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] c[ 10 ] c[ 11 ] c[ 0 ] Name of array (Note that all elements of this array have the same name, c) Position number (index of subscript) of the element within array c
Introducing arrays array is a data structure that represents a collection of the same types of data doublel] myList=new double[1 myList reference L 5.6 my List[1] 4.5 Array reference my List[2] 3.3 variable myList3] 13.2 4 Array element at index 5 e myList[5] 34.33 Element value my List 6 34 myList[7] 45.45 myList[ 8] 99993 my List 11123 Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Introducing Arrays Array is a data structure that represents a collection of the same types of data. 5.6 4.5 3.3 13.2 4 34.33 34 45.45 99.993 11123 double[] myList = new double[10]; myList reference myList[0] myList[1] myList[2] myList[3] myList[4] myList[5] myList[6] myList[7] myList[8] myList[9] Element value Array reference variable Array element at index 5