内存练习 - 综合练习Q37 E07

从 Main 的 run 方法 开始绘制内存

在每个检查点,检查你画的内存是否跟答案一致

练习 1

1 2 3 4 5 6 7 8 9 10 11 12
public class Point { public int x; public int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; }}
1 2 3 4 5 6 7 8 9
public class Main { public void run() { Point from = new Point(0, 0); Point to = new Point(3, 4); Point[] vector = {from, to}; vector[1].x = 10; // 检查点 }}
检查点 答案

练习 2

1 2 3 4 5 6 7 8 9 10 11 12
public class Point { public int x; public int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public class Driver { psvm () { new Driver().run(); } public void run() { int size = 4; Point[] points = new Point[size]; Point start = new Point(1, 1); for (int i = 0; i < size; i++) { points[i] = start; start.x += 1; } // 检查点 }}
检查点 答案

练习 3

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class Point { public int x; public int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } public Point added(int deltaX, int deltaY) { Point result = new Point(x, y); result.x += deltaX; result.y += deltaY; return result; }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public class Driver { psvm () { new Driver().run(); } public void run() { int size = 4; Point[] points = new Point[size]; Point start = new Point(1, 1); Point current = start; for (int i = 0; i < size; i++) { points[i] = current; current = current.added(1, 0); } // 检查点 }}
检查点 答案

练习 4

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class Point { public int x; public int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } public Point added(Point delta) { Point result = new Point(x, y); result.x += delta.x; result.y += delta.y; return result; }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public class Driver { psvm () { new Driver().run(); } public void run() { int size = 4; Point[] points = new Point[size]; Point start = new Point(1, 1); Point current = start; for (int i = 0; i < size; i++) { points[i] = current; current = current.added(new Point(1, 0)); } // 检查点 }}
检查点 答案

练习 5

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class Point { public int x; public int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } public Point added(Point delta) { Point result = new Point(x, y); result.x += delta.x; result.y += delta.y; return result; }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
public class Driver { psvm () { new Driver().run(); } public void run() { int size = 4; Point[] directions = new Point[size]; directions[0] = new Point(0, -1); directions[1] = new Point(1, 0); directions[2] = new Point(0, 1); directions[3] = new Point(-1, 0); Point[] points = new Point[size]; Point center = new Point(1, 1); for (int i = 0; i < size; i++) { points[i] = center.added(directions[i]); } // 检查点 }}
检查点 答案

ZZAX 微信公众

文档一更新,立刻告诉你