Java 如何将{0,0}放入数组中,并在满足if语句时对其进行更改
我的计划是创建一个只有两个值的数组,即{0,0},因为我想更改它的值以模拟坐标的移动方式。 当一个随机数被指定给x时,我想将坐标向上移动到(0,1) 假设我有一行if循环Java 如何将{0,0}放入数组中,并在满足if语句时对其进行更改,java,arrays,coordinates,element,Java,Arrays,Coordinates,Element,我的计划是创建一个只有两个值的数组,即{0,0},因为我想更改它的值以模拟坐标的移动方式。 当一个随机数被指定给x时,我想将坐标向上移动到(0,1) 假设我有一行if循环 如果(x=0){//我会将值从{0,0}更改为{0,1} 我试着这样写代码 公共静态int x=0; 公共静态int y=0; 公共静态漫游(int[]移动点){ int r=ThreadLocalRandom.current().nextInt(4); 如果(r==0){ 移动_点[x,y]=移动_点[x,y+1]; }
如果(x=0){//我会将值从{0,0}更改为{0,1}
我试着这样写代码
公共静态int x=0;
公共静态int y=0;
公共静态漫游(int[]移动点){
int r=ThreadLocalRandom.current().nextInt(4);
如果(r==0){
移动_点[x,y]=移动_点[x,y+1];
}
}
但这是不正确的,
这可能吗?怎么做?
谢谢大家! 最里面的代码必须更改为:
移动_点[1]=移动_点[1]+1代码>
1
提供移动点阵列中“y”位置的索引,但不会修改字段
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
表示变量在表达式中求值之前先递增
moving_point[1]++;
在这个房间里
我建议您不要将数组传递给函数,因为该方法不能假定其长度。您可以传递两个int或传递一个类,已经实现了几个:这只是一个示例 在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
(在保持顺序的同时提高查找效率),并通过从每个moveX
操作返回一个新实例,使Coordinate
不可变。您还需要实现equals()
和hashCode()
公共类坐标{
私人最终int x;
私人终审法院;
公共坐标(整数x,整数y){
这个.x=x;
这个。y=y;
}
公共int getX(){
返回x;
}
公共int getY(){
返回y;
}
公共坐标向上移动(整数距离){
返回新坐标(x,y+距离);
}
公共坐标向下移动(整数距离){
返回新坐标(x,y距离);
}
公共坐标向左移动(整数距离){
返回新坐标(x距离,y);
}
公共坐标向右移动(整数距离){
返回新坐标(x+距离,y);
}
@凌驾
公共字符串toString(){
返回String.format(“{%d,%d}”,x,y);
}
@凌驾
公共int hashCode(){
//我们需要做一些比“x+y”更聪明的事情,否则{1,0}可能会得到与{0,1}相同的哈希值
最终整数素数=31;
int结果=1;
结果=素数*结果+x;
结果=素数*结果+y;
返回结果;
}
@凌驾
公共布尔等于(对象obj){
如果(this==obj)返回true;
if(obj==null)返回false;
如果(getClass()!=obj.getClass())返回false;
坐标其他=(坐标)obj;
如果(x!=other.x)返回false;
如果(y!=其他.y)返回false;
返回true;
}
}
公共班级步行者{
私有LinkedHashSet路径=新LinkedHashSet();
私人协调最后;
公共步行机(坐标起点){
添加路径(起始点);
最后=起始点;
}
公共集getPath(){
返回路径;
}
公共步行{
int r=ThreadLocalRandom.current().nextInt(4);
如果(r==0){
坐标nextStep=last.moveUp(1);
if(path.add(nextStep)){
//如果添加了项,Set返回“true”,否则返回“false”。
最后一步=下一步;
}否则{
//因此,如果不加上,我们已经做到了。
//采取其他行动,重试,无论什么。。。
}
}
}
}
Walker w=新Walker(新坐标(0,0));
对于(int i=0;我希望得到建议!但是,我的计划也是将路径或坐标保存到ArrayList,这样每当我想移动到下一个点时,我就能够检查ArrayList中是否保存了重复的坐标,然后我就可以移动到另一个点,而不必返回,就像自动避免随机行走一样uld工作。你设置坐标的方式比我设置坐标的方式更可行吗?我已经添加到我的答案中。可以使用数组,但你不能将LinkedHashSet
的内置效率与equals
和hashCode
结合使用,因为你无法在你的计算机上覆盖它们ur数组,因此将限于对象相等,即包含相同数字的数组不会相等,除非它们是完全相同的实例
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());