Indexed variables The array elements are accessed through the index The array indices are o-based, i. e, it starts from 0 to arrayRefVar. length-1. In the example in Figure 5.1 mylist holds ten double values and the indices are fromo to 9 Each element in the array is represented using the following syntax, known as an indexed variable array Refvarindex] Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 11 Indexed Variables The array elements are accessed through the index. The array indices are 0-based, i.e., it starts from 0 to arrayRefVar.length-1. In the example in Figure 5.1, myList holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayRefVar[index];
Using Indexed Variables After an array is created an indexed variable can be used in the same way as a regular variable For example, the following code adds the value in myList[ol and myList[] to myList[21 myList[2]=mylist[0]+ my List[1] Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 12 Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList[0] and myList[1] to myList[2]. myList[2] = myList[0] + myList[1];
Array Initializers dEclaring, creating, initializing in one step double[ mylist={1.9,2.9,3.4,3.5}; ODo not need operator new This shorthand syntax must be in one statement double[] mylisti myList =(1.9, 2 3.4,3.5} wrong Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 13 Array Initializers ⚫Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement. double[] myList; myList = {1.9, 2.9, 3.4, 3.5}; wrong Do not need operator new
Declaring, creating, initializing USing the(D Shorthand notation double] myList={1.9,2.9,3.4,3.5} This shorthand notation is equivalent to the following statements double[ mylist =new double[4] myList[0]=1.9 my 1=2.9 my 2=3.4 myList3]=3.5 Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 14 Declaring, creating, initializing Using the Shorthand Notation double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;
Anonymous array The statement printArray(new int[3, 1, 2, 6, 4, 2) creates an array using the following syntax new data Type literal, literal, ., literalk There is no explicit reference variable for the array. Such array is called an anonymous array Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 15 Anonymous Array The statement printArray(new int[]{3, 1, 2, 6, 4, 2}); creates an array using the following syntax: new dataType[]{literal0, literal1, ..., literalk}; There is no explicit reference variable for the array. Such array is called an anonymous array