Swap I class Swap public static void swapint
Swap I class Swap { public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void swap(String s 1, String s 2) { String temp = s 1; s 1 = s 2; s 2 = temp; }
Swap II public static void swap(int[] data, int i, int j) { int temp= data[i]; data[i]= data[j]; data[j]= temp; }
Swap III public static void main(String[] args) { int a = 7, b=8; swap(a, b); System. out. println(a + " " + b); String s 1 = "hihihi", s 2 = "hahaha"; swap(s 1, s 2); System. out. println(s 1 + " " + s 2); int[] a = {0, 1 , 13, 32, 57}; swap(a, 1, 3); System. out. println(a[1] + " " + a[3]); } }
Swap III public static void main(String[] args) { int a = 7, b=8; swap(a, b); System. out. println(a + " " + b); String s 1 = "hihihi", s 2 = "hahaha"; swap(s 1, s 2); System. out. println(s 1 + " " + s 2); int[] c = {0, 1 , 13, 32, 57}; swap(c, 1, 3); System. out. println(c[1] + " " + c[3]); } }
Static fields I class New. Turtle { private static int counter = 0; private int id; New. Turtle() { ++counter; id = counter; } // // other methods //
Static fields II parallel. Paint() { tail. Down(); turn. Left(id*(360/counter)); // counter > 0 (why ? ) move. Forward(100); } public static void main(String[] args) { New. Turtle[] turtles = new New. Turtle[6]; for (int i=0; i<turtles. length; i++) { turtles[i]. parallel. Paint(); } }
Static fields II parallel. Paint() { tail. Down(); turn. Left(id*(360/counter)); // counter > 0 (why ? ) move. Forward(100); } public static void main(String[] args) { New. Turtle[] turtles = new New. Turtle[6]; for (int i=0; i<turtles. length; i++) { turtles[i] = new New. Turtle(); turtles[i]. parallel. Paint(); } }
Finalize method b b b Called when the object is destructed. System. gc() causes the JVM to do maximal effort to reclaim space from discarded objects ASAP For New. Turtle: finalize() { --counter; }
Objects as fields of other classes I class Big. Int { boolean[] number; Big. Int(boolean[] number) { this. number = number; } public static String to. String() { String result = ""; for (int i = 0; i < number. length; i++) result += number[i] ? '1' : '0'; return result; }
Objects as fields of other classes I class Big. Int { boolean[] number; Big. Int(boolean[] number) { this. number = number; } public String to. String() { String result = ""; for (int i = 0; i < number. length; i++) result += number[i] ? '1' : '0'; return result; }
Objects as fields of other classes II public static void main(String[] args) { boolean[] number = {false, false} Big. Int zero = new Big. Int(number); System. out. println(zero); number[2] = true; Big. Int one = new Big. Int(number); number[1] = true; Big. Int three = new Big. Int(number); System. out. println(zero); System. out. println(one); System. out. println(three); } }
Maze Traversal I b We can use recursion to find a path through a maze b From each location, we can search in each direction b Recursion keeps track of the path through the maze b The base case is an invalid move or reaching the final destination
Maze Traversal II public boolean traverse (int row, int column) { boolean done = false; if (valid (row, column)) // checks the cell is not blocked, not // visited and inside the matrix { grid[row][column] = TRIED; // this cell has been tried if (row == grid. length-1 && column == grid[0]. length-1) done = true; // the maze is solved else { done = traverse (row+1, column); // down if (!done) done = traverse (row, column+1); // right
Maze Traversal III if (!done) done = traverse (row-1, column); // up if (!done) done = traverse (row, column-1); // left } if (done) // this location is part of the final path grid[row][column] = PATH; } return done; }
Indirect Recursion b A method invoking itself is considered to be direct recursion b A method could invoke another method, which invokes another, etc. , until eventually the original method is invoked again b For example, method m 1 could invoke m 2, which invokes m 3, which in turn invokes m 1 again b This is called indirect recursion, and requires all the same care as direct recursion b It is often more difficult to trace and debug 15
Indirect Recursion m 1 m 2 m 3 m 1 m 2 m 1 m 3 m 2 m 3 16
- Slides: 16