160 likes | 323 Views
Swap I. class Swap { public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void swap(String s1, String s2) { String temp = s1; s1 = s2; s2 = temp; }. Swap II.
E N D
Swap I class Swap { public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void swap(String s1, String s2) { String temp = s1; s1 = s2; s2 = 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 s1 = "hihihi", s2 = "hahaha"; swap(s1,s2); System.out.println(s1 + " " + s2); 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 s1 = "hihihi", s2 = "hahaha"; swap(s1,s2); System.out.println(s1 + " " + s2); int[] c = {0, 1 ,13, 32, 57}; swap(c, 1, 3); System.out.println(c[1] + " " + c[3]); } }
Static fields I class NewTurtle { private static int counter = 0; private int id; NewTurtle() { ++counter; id = counter; } // // other methods //
Static fields II parallelPaint() { tailDown(); turnLeft(id*(360/counter)); // counter > 0 (why ?) moveForward(100); } public static void main(String[] args) { NewTurtle[] turtles = new NewTurtle[6]; for (int i=0; i<turtles.length; i++) { turtles[i].parallelPaint(); } }
Static fields II parallelPaint() { tailDown(); turnLeft(id*(360/counter)); // counter > 0 (why ?) moveForward(100); } public static void main(String[] args) { NewTurtle[] turtles = new NewTurtle[6]; for (int i=0; i<turtles.length; i++) { turtles[i] = new NewTurtle(); turtles[i].parallelPaint(); } }
Called when the object is destructed. System.gc() causes the JVM to do maximal effort to reclaim space from discarded objects ASAP For NewTurtle: finalize() { --counter; } Finalize method
Objects as fields of other classes I class BigInt { boolean[] number; BigInt(boolean[] number) { this.number = number; } public static String toString() { 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 BigInt { boolean[] number; BigInt(boolean[] number) { this.number = number; } public String toString() { 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, false} BigInt zero = new BigInt(number); System.out.println(zero); number[2] = true; BigInt one = new BigInt(number); number[1] = true; BigInt three = new BigInt(number); System.out.println(zero); System.out.println(one); System.out.println(three); } }
We can use recursion to find a path through a maze From each location, we can search in each direction Recursion keeps track of the path through the maze The base case is an invalid move or reaching the final destination Maze Traversal I
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 II
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; } Maze Traversal III
A method invoking itself is considered to be direct recursion A method could invoke another method, which invokes another, etc., until eventually the original method is invoked again For example, method m1 could invoke m2, which invokes m3, which in turn invokes m1 again This is called indirect recursion, and requires all the same care as direct recursion It is often more difficult to trace and debug Indirect Recursion
m1 m2 m3 m1 m2 m3 m1 m2 m3 Indirect Recursion