Look if an array has an specified object

和自甴很熟 提交于 2019-12-12 01:48:55

问题


I have an array composed of an X number of 2DPoints, and my goal is to do a boolean operation that could check if that array has the specified 2DPoint. Something like this:

Point2D.Double arrayPoints[] = new Point2D.Double[numberOfPoints];
Point2D.Double pointPVariable = new Point2D.Double(positionXVariable,positionYVariable);
arrayPoints[variableNumber] = pointPVariable;

if(arrayPoints has the Point2D(2.45,6.52)){
    do this
}

How can I do that boolean operation?? Thank you very much!


回答1:


Arrays.asList(arrayPoints).contains(new Point2D.Double(2.45,6.52))

This works as long as the classes being compared override the equals method.




回答2:


If your array is sorted with the natural ordering of Point2D.Double, you can use the Arrays.binarySearch method.

if (Arrays.binarySearch(arraysPoints, new Point2D.Double(2.45,6.52)) >= 0) {
    do this
}


来源:https://stackoverflow.com/questions/7702853/look-if-an-array-has-an-specified-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!