- Searching through a 2D array for a particular element.
1
2 public class ArraySearch {
3 public static void main(String[] args) {
4 String[][] str = {
5 { "a", "b", "c", "d" },
6 { "e", "f", "g", "h" }
7 };
8
9 String goal = "c";
10
11 boolean error = true;
12 for (int i=0; i<str.length; i++) {
13 for (int j=0; j<str[i].length; j++) {
14 if (str[i][j].equals(goal)) {
15 error = false;
16 System.out.println("row = " + i);
17 System.out.println("col = " + j);
18 break;
19 }
20 }
21 if (!error) {
22 break;
23 }
24 }
25 if (error) {
26 System.out.println("Error");
27 }
28 }
29 }
30
31
- Running out of memory
1
2 public class OutOfMemory {
3 public static void main(String[] args) {
4 int[] x = new int[1];
5 while (true) {
6 int y = x.length;
7 y = y * 2;
8 System.out.println(y);
9 x = new int[y];
10 }
11 }
12 }
13
14
No comments:
Post a Comment