Java:在对象数组中搜索 2 个特定对象值

发布于 2024-09-30 19:43:40 字数 328 浏览 3 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沩ん囻菔务 2024-10-07 19:43:40

执行此操作的方法是以某种方式排列数组中的 Cell 对象,以便存在从 X,Y 坐标到数组中 Cell 的索引的简单映射。

例如,假设 X 和 Y 从 1 到 10。假设我们然后排列 Cells,以便:

array[0] = Cell(1, 1);
array[1] = Cell(1, 2);
...
array[9] = Cell(1, 10);
array[10] = Cell(2, 1);
array[11] = Cell(2, 2);
...
array[99] = Cell(10, 10);

应该很容易看出我们可以计算 Cell(i,j) 在数组中的索引并获取cell 如下:

public Cell getCell(Cell[] array, int i, int j) {
    int index = (10 * (i - 1)) + (j - 1);
    return array[index];
}

这是支持 N 维数组类型的编程语言通常用来实现它们的方法。

可以对此进行简单修改以处理以下情况:

  • 常数 10 是其他
  • 矩阵 不是方阵
  • 矩阵有两个以上维度
  • 索引从 0 到 N - 1 而不是 1 到 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:

array[0] = Cell(1, 1);
array[1] = Cell(1, 2);
...
array[9] = Cell(1, 10);
array[10] = Cell(2, 1);
array[11] = Cell(2, 2);
...
array[99] = Cell(10, 10);

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:

public Cell getCell(Cell[] array, int i, int j) {
    int index = (10 * (i - 1)) + (j - 1);
    return array[index];
}

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:

  • the constant 10 is something else
  • the matrix is not square,
  • the matrix has more than two dimensions
  • indexes run from 0 to N - 1 instead of 1 to N
  • etcetera

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.

〃安静 2024-10-07 19:43:40

听起来(无论如何,如果您想使用二分搜索)您正在使用 x = 0, y = 0; 将元素 0 设置为单元格;元素 1 到 x = 0, y = 1 等。如果是这样,您应该能够轻松计算给定单元格的确切索引:

// contains the Cell with x = desiredX, y = desiredY
yourArray[desiredX * X + desiredY];

但是,如果这就是您正在做的事情,那么它' d 制作一个二维数组可能更简单:

yourArray = new Cell[X][Y];
...
yourArray[desiredX][desiredY];

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 to x = 0, y = 1, etc. If so you should be able to trivially compute the exact index of a given Cell:

// contains the Cell with x = desiredX, y = desiredY
yourArray[desiredX * X + desiredY];

If this is what you're doing, however, it'd probably be simpler to just make a 2-dimensional array:

yourArray = new Cell[X][Y];
...
yourArray[desiredX][desiredY];
時窥 2024-10-07 19:43:40

上面的两个答案显示了快速获取数组索引的简单方法。我想提出一个替代方案——使用带有键、值对的哈希图。该值可以是对象。访问哈希图元素在恒定时间内运行..

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文