Java:在对象数组中搜索 2 个特定对象值
我正在用 Java 做一个项目,其中包括 (x,y) 坐标。 我创建了一个 Cell 类,它保护整数 X &是; 初始化后,我执行一个 for 循环,通过将 X 和 X 相乘来设置单元格数组。 Y 由用户给出,假设如果 X= 10 且 Y = 10,我将创建一个单元格数组[100]。
但是,如何快速搜索数组,而不执行 for 循环并多次检查每个单独的值?
假设我正在寻找包含 X=5 & 的对象y = 3。 我知道我可以通过 for 循环查找具有值 x 和 y 的对象,但我想知道是否有一种方法可以进行二分搜索并找到“更快一点”包含 X=5 的对象[i] Y=5。
非常感谢。
I'm doing a project in Java which includes (x,y) coordinates.
I have created a class of Cell which has protected integers X & Y;
Upon initialization, i do a for loop which sets an array of cell by multiplying the X & Y given by the user, say if X= 10 and Y = 10, i create an array of cells[100].
However, how can i search the array fast, without doing a for loop and checking each individual value very time?
Say I'm looking for the object that contains X=5 & y = 3.
I know i can go through with a for loop looking for object with values x and y, but i was wondering if there is a way to do a binary search and find "a bit faster" the object[i] that contains X=5 and Y=5.
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行此操作的方法是以某种方式排列数组中的 Cell 对象,以便存在从 X,Y 坐标到数组中 Cell 的索引的简单映射。
例如,假设 X 和 Y 从 1 到 10。假设我们然后排列 Cells,以便:
应该很容易看出我们可以计算 Cell(i,j) 在数组中的索引并获取cell 如下:
这是支持 N 维数组类型的编程语言通常用来实现它们的方法。
可以对此进行简单修改以处理以下情况:
还有各种其他方法你可以用 Java 来表示二维矩阵。最简单的一种是使用
Cell[][] cells
,它允许您以cells[i-1][j-1]
的形式访问单元格。如果矩阵稀疏(即单元丢失),可以设计更复杂的表示,使用更少的空间,但代价是更复杂的代码和更慢的访问时间。The way to do this is to arrange the Cell objects in the array in a way so that there is a simple mapping from an X,Y coordinate to the Cell's index in the array.
For example, lets assume that X and Y go from 1 to 10. Suppose that we then arrange the Cells so that:
It should be easy to see that we can calculate the index of Cell(i,j) in the array and fetch the cell as follows:
This is the approach that programming languages that support N-dimensional array types typically use to implement them.
This can be trivially modified to deal with cases where:
There are various other ways that you could represent 2-D matrices in Java. The simplest one is just using a
Cell[][] cells
which allows you to access cells as (for example)cells[i-1][j-1]
. More complicated representations can be designed that use less space if the matrix is sparse (i.e. cells are missing) at the cost of more complex code and slower access times.听起来(无论如何,如果您想使用二分搜索)您正在使用
x = 0, y = 0
; 将元素 0 设置为单元格;元素 1 到x = 0, y = 1
等。如果是这样,您应该能够轻松计算给定单元格的确切索引:但是,如果这就是您正在做的事情,那么它' d 制作一个二维数组可能更简单:
It sounds like (if you want to use binary search, anyway) you're setting element 0 to the Cell with
x = 0, y = 0
; element 1 tox = 0, y = 1
, etc. If so you should be able to trivially compute the exact index of a given Cell:If this is what you're doing, however, it'd probably be simpler to just make a 2-dimensional array:
上面的两个答案显示了快速获取数组索引的简单方法。我想提出一个替代方案——使用带有键、值对的哈希图。该值可以是对象。访问哈希图元素在恒定时间内运行..
the above two answers show the trivial method for getting the array index fast. id like to propose an alternative- use hashmaps with key, value pairings. the value could be objects. accessing hashmap elements run in constant time..