在数组中存储坐标并在java中比较它们的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21938141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:30:31  来源:igfitidea点击:

best way to store coordinates in arrays and comparing them in java?

javaarrayslistsortingdouble

提问by user3337754

Asking for java help

寻求Java帮助

I am 1.5 weeks into programming so try and make an answer easy for an idoit. And this answer is probably out there and I am probably to dumb to realize I read it already.

我有 1.5 周的编程时间,所以试着让一个 idoit 的答案变得容易。这个答案可能就在那里,我可能愚蠢到意识到我已经读过它了。

So I have a bunch of object instances and I want to add Instance.position which is

所以我有一堆对象实例,我想添加 Instance.position 这是

double[] position = (x,y);

to some sort of array/list/stack so I can the check to see if a different object Instance.position is the same as one I set to the array/stack/list.
This is what i have:

到某种数组/列表/堆栈,以便我可以检查不同的对象 Instance.position 是否与我设置到数组/堆栈/列表的对象相同。
这就是我所拥有的:

public ArrayList<double[]> objPositions = new ArrayList<double[]>();

public ArrayList<double[]> objPositions = new ArrayList<double[]>();

public boolean isThereAObj(double[] userPosition){if (objPositions.contains(userPosition));return true;}this does not work.. (i think)

public boolean isThereAObj(double[] userPosition){if (objPositions.contains(userPosition));return true;}这不起作用..(我认为)

where the array list has already been defined earlier and ideally my array list could be of objects and i could check the objects element position with my comparing objects position.

之前已经定义了数组列表,理想情况下,我的数组列表可以是对象,我可以使用比较对象位置检查对象元素位置。

So basically I want to have a list with all instances of all objects I create in my program and then I want to be able to compare one or more of their elements to a different object (not on the list)

所以基本上我想要一个列表,其中包含我在程序中创建的所有对象的所有实例,然后我希望能够将它们的一个或多个元素与不同的对象(不在列表中)进行比较

采纳答案by Marco13

There are several issues here. First, it is not very elegant to describe a 2D point as a 2-element double[]array. Instead, you should consider using java.awt.geom.Point2Dobjects:

这里有几个问题。首先,将 2D 点描述为 2 元素double[]数组不是很优雅。相反,您应该考虑使用java.awt.geom.Point2D对象:

Point2D p = new Point2D.Double(x,y);

This makes the implementation of the method that you are looking for much easier:

这使得您正在寻找的方法的实现更容易:

private final List<Point2D> wallPositions = new ArrayList<Point2D>(); 
public boolean isThereAWall(Point2D userPosition) { 
    return wallPositions.contains(userPosition); 
}

The problem with your current attempt is that double[]arrays do not know the concept of "equality" that you are looking for. (More formally: There is no appropriate implementation of the Object#equals(Object)method for them)

您当前尝试的问题在于double[]数组不知道您正在寻找的“平等”概念。(更正式地说:没有Object#equals(Object)为他们适当的方法实现)

However, if you want to stick to your double arrays (although I do notrecommend this), you could do something like

但是,如果您想坚持使用双数组(尽管我推荐这样做),您可以执行以下操作

private final List<double[]> wallPositions = new ArrayList<double[]>(); 

public boolean isThereAWall(double[] userPosition) { 
    for (double entry[] : wallPositions) {
        if (Arrays.equal(entry, userPosition)) {
            return true;
        }
    }
    return false;
}

But there may (in bothcases) be another caveat, namely the limited precision of doublevalues. There are veryfew cases where it is appropriate to compare doublevalues for identity (with ==). But whether this really is a problem here depends on your application case.

但是(在这两种情况下)可能还有另一个警告,即double值的有限精度。在少数情况下,比较double身份(与==)的值是合适的。但这是否真的是这里的问题取决于您的应用案例。

回答by Martin Dinov

Use Point2D.Double, which is exactly meant to store 2 doubles in a Pointobject. You can keep multiple points in an ArrayList, as you are now. To check whether a point already exists, you'd just loop through it and call .equals(newPoint)to compare the points. However, as doubleprecision points might not ever equal each other, you probably want to check whether two points are within a certain small distance from each other (again, with equals()for example).

使用Point2D.Double,这正是为了double在一个Point对象中存储 2秒。您可以ArrayList像现在一样在 中保留多个点。要检查一个点是否已经存在,您只需遍历它并调用.equals(newPoint)以比较这些点。但是,由于double精度点可能永远不会彼此相等,因此您可能想要检查两个点是否在彼此之间的某个小距离内(再次,equals()例如)。