Arrays
Arrays
1. Understanding code
public class Array-Assignment {
public static void main(String [] args) {
int [] x = new int[3];
int [] y = {3, 5, 9, 2};
x[2] = y[3];
x[0]++;
y[1] += y[2] * y[0];
int [] z = x;
x = y;
} x y z
}
1 0 2
3 32 9 2
0
public class Array-With-Loop1 {
public static void main(String [] args) {
int [] x = {-4, 9, 8, 2, -5, 7, 1};
for(int i=1; i<x.length; i++) {
x[i] += x[i-1]; // notice: += instead of =
}
}
}
x i
7
-4 5 13 15 10 17 18
public class Array-With-Loop2 {
public static void main(String [] args) {
int [] x = {-4, 9, 8, 2, -5, 7, 1};
for(int i=1; i<x.length; i++) {
x[i] = x[i-1];
}
}
}
x i
7
-4 -4 -4 -4 -4 -4 -4
-4 9 8 2 -5 7 1
3. Read 500 ints from the keyboard, and store them in an array. Display "true" on the
screen if there is an even number of even numbers among these 500. Otherwise, display
"false".