Unit 9
//Hack 1
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "a", "f", "g" },
{ "b", "e", "h" },
{ "c", "d", "i" }
};
// Print the last element in the array!
System.out.println(arr[2][2]);
}
}
Test.main(null);
public class Test {
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// Change Austin to Athens and print!
System.out.println("Change Austin to Athens and print!");
}
arr[2][0] = "Athens";
}
Test.main(null);
public class Test {
int longest = 0;
public static void main(String[] args) {
String[][] arr = {
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
// String longest = arr[0][0];
// Use nested for loops to find the longest or shortest string!
System.out.println("Use nested for loops to find the longest or shortest string!");
}
for (String[] row : arr) {
for (String[] item : row) {
if (array[row][item].length() > longest) {
longest = array[row][item].length();
}
}
}
System.out.println(longest);
}
Test.main(null);
int height = 1;
int spaceCounter = 0;
int leafCounter = 0;
String[][] tree = new String[height + 1][height * 2 ];
for (int i = 0; i < tree.length; i++) {
int maxLeaf = i * 2 + 1;
for (int j = 0; j < tree[height].length; j++) {
if (j < (2*(height - 1 - i))) {
tree[i][j] = " ";
} else if (j % 2 == 0 && leafCounter <= maxLeaf) {
tree[i][j] = "*";
leafCounter++;
} else if (j % 2 != 0 && leafCounter <= maxLeaf) {
tree[i][j] = " ";
} else {
tree[i][j] = " ";
}
}
}
System.out.println(tree);