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