2D Array
popcorn hacks for objects
int[][] exampleArray = {
{"Hello", "World"},
{"Java", "Array"}
};
This should be a String not an int
public class ArrayPractice {
public static void main(String[] args) {
// Write the code to declare and initialize the 2D array here
int[][] myNumbers = new int[3][];
myNumbers[1] = {1,2,3};
myNumbers[2] = {4,5,6};
myNumbers[0] = {7,8,9};
// Print the array
System.out.println(java.util.Arrays.deepToString(myNumbers));
}
}
ArrayPractice.main(null)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
String[][] hack = {
{"Hello", "World"},
{"Java", "Array"}
};
hack[1][0] = "Programming";
System.out.println(Arrays.deepToString(hack));
[[Hello, World], [Programming, Array]]
public class Main {
public static void main(String[] args) {
int find[][] = {
{10, 20, 30},
{40, 55, 60},
{70, 80, 90},
};
int last = 0;
int lastDiff = find[0][0];
for (int i = 0; i<find.length; i++){
for(int j = 0; j<find[i].length;j++){
if(find[i][j] - last != lastDiff){
System.out.println("Value " + (find[i][j]) +" doesn't match the pattern");
last = last + lastDiff;
}
else {
last = find[i][j];
}
}
}
}
}
Main.main(null);
Value 55 doesn't match the pattern
public class Main {
public static void main(String[] args) {
int[][] array = {
{1,2,3},
{4,5,6},
{7,8,9}
};
findIndexOfTarget(array,9);
}
public static int[] findIndexOfTarget(int[][] array, int target){
int rows = array.length;
if (rows == 0) return new int[] {-1, -1};
int cols = array[0].length;
int left = 0;
int right = rows * cols - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
System.out.println(mid);
int midValue = array[mid / cols][mid % cols];
if (midValue == target) {
return new int[] {mid / cols, mid % cols}; // Return the row and column index
}
if (midValue < target) {
left = mid + 1; // Move to the right half
} else {
right = mid - 1; // Move to the left half
}
}
return new int[] {-1, -1}; // Target not found
}
}
Main.main(null);
4
6
7
8
Homework hacks page
8.1
public class Main {
public static void main(String[] args) {
String[][] array = {
{"My", "A"},
{"AP", "Class"},
{"CS", "Rocks!"}
};
for (int col = 0; col < array[0].length; col++) {
for (int row = 0; row < array.length; row++) {
System.out.print(array[row][col] + " ");
}
}
}
}
Main.main(null);
THe answer to the multiple choice is B. We are looping through each collumn followed by each row.
8.2
public class GradeSearch {
public static String searchGrade(String[][] grades, String name) {
for (int row = 0; row < grades.length; row++){
// fif the name we are trying to find matches the first item in row
if (name == grades[row][0]){
//then return the second item (the grade)
return grades[row][1];
}
}
return "Student not found";
}
public static void main(String[] args) {
// Sample 2D array with names and grades
String[][] grades = {
{"John", "93"},
{"Alice", "85"},
{"Bob", "78"},
{"Eve", "92"}
};
// Test the search function
String nameToSearch = "Alice";
String grade = searchGrade(grades, nameToSearch);
System.out.println(nameToSearch + "'s grade: " + grade);
nameToSearch = "Charlie";
grade = searchGrade(grades, nameToSearch);
System.out.println(nameToSearch + "'s grade: " + grade);
}
}
// Execute the main method to see the output
GradeSearch.main(null);
Alice's grade: 85
Charlie's grade: Student not found