Creating and initialize array New dataType d literal, literal,, literalk] double[ mylist =1, 2,3: my List =new double[ 1.9, 2.9, 3.4, 3.5 for(int i=1; i<11; i++) doublel my List=(1, 2, 3j my List =new double[f1. 9, 2.9, 3.4, 3.5i System. out. printIn(my List); Demo TestAnounymous java Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 16 Creating and initialize array New dataType [] {literal0, literal1,…,literalk} double[] myList = {1 , 2 , 3}; myList = new double[]{1.9, 2.9, 3.4, 3.5}; for (int i = 1; i < 11 ; i ++ ) { double[] myList = {1 , 2 , 3}; myList = new double[]{1.9, 2.9, 3.4, 3.5}; System.out.println(myList); } Demo TestAnounymous.java
Trace Program with Arrays Declare array variable values, create an array, and assign its reference to values public class Test public static void main(Str s args)( After the array is created int[ values=new int[5]: for(int 1=1; 1<5; 1++)i values[]=1+ valuesi-1] 30 values[]= values[ 1]+ values[4] Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 17 Trace Program with Arrays public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } Declare array variable values, create an array, and assign its reference to values After the array is created 0 1 2 3 4 0 0 0 0 0
Trace Program with arrays After first iteration of the loop i is 2 public class Test public static void main(StriN /gs)( After the first iteration int[ values=new int[] 0 for (int 1=1; 1<5; 1++) values=i+ values i-I 000 values[]= values[ 1]+ values[4] Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 18 Trace Program with Arrays public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } After first iteration of the loop, i is 2 After the first iteration 0 1 2 3 4 0 1 0 0 0
Trace Program with arrays After second iteration of the loop i is 3 public class Test public static void main(StriN /gs)( After the second iteration int[ values=new int[] 0 for (int 1=1; 1<5; 1++) values=i+ values i-I 0 values[]= values[ 1]+ values[4] Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 19 Trace Program with Arrays public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } After second iteration of the loop, i is 3 After the second iteration 0 1 2 3 4 0 1 3 0 0
Trace Program with arrays After third iteration of the loop i is 4 public class Test public static void main(StriN /gs)( After the third iteration int[ values=new int[] 0 for (int 1=1; 1<5; 1++) values=i+ values i-I 360 values[]= values[ 1]+ values[4] Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 20 Trace Program with Arrays public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } After third iteration of the loop, i is 4 After the third iteration 0 1 2 3 4 0 1 3 6 0