我的计划是创建一个仅包含两个值的数组,即{0,0},因为我想更改其值以模拟坐标的移动方式。 当将随机数分配给x时,我想将坐标向上移动到(0,1)
假设我有一行if循环,
[ { name: 'projects/xxxxproj-xxxx/agent/sessions/xxxxxx-xxxxxx-xxxxx-xxxxx/contexts/xxxxxxx-context' } ]
我试图以这种方式编写代码,
if(x=0){ //I would change the value from {0,0} to {0,1} }
但不正确,
这可能吗?如何? 谢谢!
答案 0 :(得分:1)
最里面的代码必须更改为:
moving_point[1] = moving_point[1] + 1;
1
为您提供了Moving_point数组中“ y”位置的索引,但不会修改您的字段。
答案 1 :(得分:0)
public static int x = 0;
public static int y = 0;
public static void randomWalk(int [] moving_point) {
int r = ThreadLocalRandom.current().nextInt(4);
if(r == 0) {
moving_point[x,y] = moving_point[x,++y];
}
}
在这种情况下,我建议使用Pre-Increment(++x
)。 Pre-increment
表示变量在表达式中求值之前先递增。
答案 2 :(得分:0)
答案 3 :(得分:0)
在Java中表示坐标的“更正确”的方法是创建一个坐标类:
public class Coordinate {
private int x;
private int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void moveUp(int distance) {
y += distance;
}
public void moveDown(int distance) {
y -= distance;
}
public void moveLeft(int distance) {
x -=distance;
}
public void moveRight(int distance) {
x += distance;
}
@Override
public String toString() {
return String.format("{%d,%d}", x, y);
}
}
public static void randomWalk(Coordinate point) {
int r = ThreadLocalRandom.current().nextInt(4);
if(r == 0) {
point.moveUp(1);
}
}
这使您可以简洁地表示软件中的多个坐标,并允许您编写一些非常直观的方法,例如上例中的moveUp/Down/Left/Right
。
执行此操作的另一个很好的理由是数组上没有约束,因此有人可能会无意中添加第三,第四或第五个值,这显然是没有意义的。使用单独的类可使代码更具自记录性。
回到示例,您现在可以运行:
Coordinate c = new Coordinate(0, 0);
System.out.println(c); // prints "{0,0}"
randomWalk(c);
System.out.println(c); // prints "{0,1}" IF you were lucky to get a random 0...
注意:
在这里,坐标空间的解释就像在学校的图形上一样,向右增加x并向上增加y。在计算机屏幕上,y空间通常是翻转的(原点在屏幕的左上角),因此在练习中请考虑如何进行纠正。 (提示,仅涉及更改moveUp()
和moveDown()
)
编辑:
根据您的评论,为了能够记录路径,您可能希望使用LinkedHashMap
而不是ArrayList
(在保持顺序的同时提高查找效率)并使Coordinate
不可变从每个moveX
操作返回一个新实例。您还需要实现equals()
和hashCode()
。
public class Coordinate {
private final int x;
private final int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Coordinate moveUp(int distance) {
return new Coordinate(x, y+distance);
}
public Coordinate moveDown(int distance) {
return new Coordinate(x, y-distance);
}
public Coordinate moveLeft(int distance) {
return new Coordinate(x-distance, y);
}
public Coordinate moveRight(int distance) {
return new Coordinate(x+distance, y);
}
@Override
public String toString() {
return String.format("{%d,%d}", x, y);
}
@Override
public int hashCode() {
//we need to do something a bit more clever than "x+y", otherwise {1,0} might end up with the same hash as {0,1}
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null) return false;
if(getClass() != obj.getClass()) return false;
Coordinate other = (Coordinate)obj;
if(x != other.x) return false;
if(y != other.y) return false;
return true;
}
}
public class Walker {
private LinkedHashSet<Coordinate> path = new LinkedHashSet<>();
private Coordinate last;
public Walker(Coordinate startingPoint) {
path.add(startingPoint);
last = startingPoint;
}
public Set<Coordinate> getPath() {
return path;
}
public void randomWalk() {
int r = ThreadLocalRandom.current().nextInt(4);
if(r == 0) {
Coordinate nextStep = last.moveUp(1);
if(path.add(nextStep)) {
//Set returns "true" if the item was added, "false" otherwise.
last = nextStep;
} else {
//So if not added, we've already been there.
//take alternative action, retry, whatever...
}
}
}
}
Walker w = new Walker(new Coordinate(0,0));
for(int i=0; i<10; i++) {
w.randomWalk();
}
System.out.println(w.getPath());