int[][] arr = {
{1, 2},
{3, 4},
{5, 6}
};
for (int[] row : arr) {
row[0] *= 2;
}
for (int[] row : arr) {
for (int val : row) {
System.out.print(val + " ");
}
}
2 2 6 4 10 6
This expression gives the number of columns in a rectangular 2D array called arr.
What is arr[0].length?
This is what happens when you try to access a value at an index that doesn't exist for an array.
What is IndexOutofBounds Runtime Error?
Key words used when creating objects.
What is new?
What is the Outuput?
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[r].length; c++) {
if (r == c) {
System.out.print(arr[r][c] + " ");
}
}
}
1 5 9
What does this code output?
int[][] arr = {
{1, 2, 3},
{4, 5, 6}
};
for (int[] row : arr) {
for (int val : row) {
val += 5;
}
}
for (int[] row : arr) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
1 2 3
4 5 6
Default values for values stored by int array objects if not explicitly specified
What is 0?
Key word to indicate that a field should be shared across objects. Also indicates that a method can be called without creating an object beforehand.
What is static?
What is the output?
int[][] arr = {
{3, 1, 4},
{1, 5, 9},
{2, 6, 5}
};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i][i] + " ");
}
3 5 5
What type of loop would be best to use when summing values in each column.
Column Major
The result of:
Pokemon[] p1 = {new Pokemon("mew", 200), new Pokemon("pichu", 20)};
Pokemon[] p2 = {new Pokemon("mew", 200), new Pokemon("pichu", 20)};
System.out.println(p1 == p2);
What is false?
This Java keyword can be used on a class, method, or variable to make it accessible from any other class.
What is public?
What is the output?
int[][] arr = {
{2, 4, 6},
{1, 3, 5},
{7, 8, 9}
};
int sum = 0;
for (int r = 0; r < arr.length; r++) {
sum += arr[r][0];
}
System.out.println(sum);
10
This expression accesses the bottom-right element of a rectangular 2D array.
What is arr[arr.length - 1][arr[0].length - 1]?
The result of:
int[] nums = {1, 2, 3};
for(int i = 0; i < 10; i--){
System.out.println(nums[i] + 2);
}
What is error?
This keyword is used to refer to the current object inside a class.
What is this?
The result of this:
Pokemon[] myTeam = {new Pokemon("mew", 200), new Pokemon("gible", 30)};int[] nums = {5, 5, 5};
for(int num: nums){
num = 10;
}
for(Pokemon mon: myTeam){
mon.editHp(-10);
}
System.out.println(nums[0]);
System.out.println(myTeam[0].getHp());
What is:
5
190
?
The result (what nums ends up storing) of this:
int[][] nums = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for(int i = 2; i >= 0; i--){
for(int j = 2; j >= 0; j--){
if(!(i == 0 && j == 0)){
if(j != 0){
nums[i][j] += nums[i][j - 1];
}
else{
nums[i][j] += nums[i - 1][2];
}
}
}
}
The result is:
[[1, 3, 5],
[7, 9, 11],
[13, 15, 17]]
Default values for values stored by non primitive type array objects if not explicitly specified
What is null?
Keyword that makes it so that you can only read from a variable once it is initially assigned a value.
What is final?