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
Declaring A rray variables datatype[ array Refvar Example Declaring dosent allocate memory for array but for doublel mylist the reference o datatype array RefVar[; //This style is correct, but not referred Example double my list[ when declaring, we cant give the number of the element int c [23] is wrong; Liang, Introduction to Java Programming, reused by Dal-Kalyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Declaring Array Variables ⚫ datatype[] arrayRefVar; Example: double[] myList; ⚫ datatype arrayRefVar[]; // This style is correct, but not preferred Example: double myList[]; Declaring dosen’t allocate memory for array, but for the reference When declaring , we can’t give the number of the element int c [23] is wrong;
Creating arrays arrayRef Var = new datatypelarraySize] Example mylist =new double[10] ● Subscript O Also called an index O Position number in square brackets O Must be integer or integer expression my List[O] references the first element in the array myList[9] references the last element in the array Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Creating Arrays arrayRefVar = new datatype[arraySize]; Example: myList = new double[10]; ⚫ Subscript Also called an index Position number in square brackets Must be integer or integer expression myList[0] references the first element in the array. myList[9] references the last element in the array
Declaring and Creating in One step o datatype arrayRefvar=new datatype[array Size doublel mylist=new double[10 o datatype array Ref var=new datatype array Size double my list[=new double[10] Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Declaring and Creating in One Step ⚫ datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10]; ⚫ datatype arrayRefVar[] = new datatype[arraySize]; double myList[] = new double[10];
The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar length Compare with String For example, my List length returns 10 Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length For example, myList.length returns 10 Compare with String
Default values When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, 1uo0oo' for char types, and false for boolean types Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Default Values When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, '\u0000'for char types, and false for boolean types