我的对象的 ArrayList,indexOf 问题

发布于 2024-08-31 11:28:27 字数 127 浏览 3 评论 0原文

我对 Java 的 ArrayList 有问题。我创建了一个对象,它包含两个属性,x 和 y。现在我已经在 ArrayList 中加载了一些对象。问题是我不知道如何找到我正在搜索的具有 x 属性的某个对象的索引。有什么办法可以做到这一点吗?

I have problem with Java's ArrayList. I've created an Object, that contains two attributes, x and y. Now I've loaded some object in my ArrayList. Problem is that I don't know how to find index of some object with x atribute I'm searching. Is there any way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

时光倒影 2024-09-07 11:28:27

假设如下:

public class Point {
   public final int x;
   public final int y;
}

以及以下声明:

List<Point> points = ...;

您可以使用 for-each 迭代所有点并找到您想要的点:

for (Point p : points) {
   if (p.x == targetX) {
      process(p);
      break; // optional
   }
}

请注意,这不会为您提供 索引,但它会给您Point 本身,有时这就足够了。如果您确实需要索引,那么您需要使用索引 for 循环,使用 size()get(int index) (请参阅 BalusC 的答案)。

另请参见


上述解决方案在 O(N) 中搜索每个 targetX。如果您经常这样做,那么您可以通过声明 class Point ImplementsComparable,使用 x 作为 Collections.sort

然后您可以Collections.binarySearch。由于设置时间为 O(N log N),现在可以在 O(log N) 内回答每个查询。

另一种选择是使用 SortedSet例如 TreeSet,特别是如果您拥有的是 设置,而不是List

另请参阅

Assuming something like:

public class Point {
   public final int x;
   public final int y;
}

And a declaration of:

List<Point> points = ...;

You can use for-each to iterate through all the points and find the one you want:

for (Point p : points) {
   if (p.x == targetX) {
      process(p);
      break; // optional
   }
}

Note that this will not give you the index, but it will give you the Point itself, which sometimes is enough. If you really need the index, then you'd want to use indexed for loop, using size() and get(int index) (see BalusC's answer).

See also


The above solution searches in O(N) for each targetX. If you're doing this often, then you can improve this by declaring class Point implementsComparable<Point>, using x as the primary sorting key for Collections.sort.

Then you can Collections.binarySearch. With a setup time of O(N log N), each query can now be answered in O(log N).

Another option is to use a SortedSet such as a TreeSet, especially if what you have is a Set<Point>, not a List<Point>.

See also

白衬杉格子梦 2024-09-07 11:28:27

这是您要找的吗?

public class Point {

private final int x;
private final int y;

public Point(int x, int y) {
    this.x = x;
    this.y = y;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

@Override
public boolean equals(Object o) {
    return (o instanceof Point && getX() == ((Point) o).getX() && getY() == ((Point) o)
            .getY());

}

如果

public class TestIndexOf {

public static void main(String[] args){
    Point p1 = new Point(10,30);
    Point p2 = new Point(20,40);
    Point p3 = new Point(50,40);
    Point p4 = new Point(60,40);
    List<Point> list = new ArrayList<Point>();
    list.add(p1);
    list.add(p2);
    list.add(p3);
    list.add(p4);
    System.out.println(list.indexOf(p3));
}

您只想搜索 x 属性,请更改 equals 方法以仅比较 x 值,例如

@Override
public boolean equals(Object o) {
    return (o instanceof Point && getX() == ((Point) o).getX());

}

Is this what you looking for?

public class Point {

private final int x;
private final int y;

public Point(int x, int y) {
    this.x = x;
    this.y = y;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

@Override
public boolean equals(Object o) {
    return (o instanceof Point && getX() == ((Point) o).getX() && getY() == ((Point) o)
            .getY());

}

}

public class TestIndexOf {

public static void main(String[] args){
    Point p1 = new Point(10,30);
    Point p2 = new Point(20,40);
    Point p3 = new Point(50,40);
    Point p4 = new Point(60,40);
    List<Point> list = new ArrayList<Point>();
    list.add(p1);
    list.add(p2);
    list.add(p3);
    list.add(p4);
    System.out.println(list.indexOf(p3));
}

}

If you just want to search on the x property, change the equals method to compare only the x values like:

@Override
public boolean equals(Object o) {
    return (o instanceof Point && getX() == ((Point) o).getX());

}
美男兮 2024-09-07 11:28:27

只需迭代列表并测试每个元素即可。

for (int i = 0; i < list.size(); i++) {
    if (list.get(i).getX() == someValue) { // Or use equals() if it actually returns an Object.
        // Found at index i. Break or return if necessary.
    }
}

冗长,是的,但可能直到 JDK7 带有 闭包 为止,没有其他标准方法。

Just iterate over the list and test every element.

for (int i = 0; i < list.size(); i++) {
    if (list.get(i).getX() == someValue) { // Or use equals() if it actually returns an Object.
        // Found at index i. Break or return if necessary.
    }
}

Verbose, yes, but possibly until JDK7 with Closures, there is no other standard way.

痞味浪人 2024-09-07 11:28:27

如果我希望能够根据一个特定的属性值从集合中获取对象,我通常只使用映射。我发现这比遍历列表更干净。

Map<String, Object> map = new HashMap<String, Object>();

map.put(o1.getX(), o1);
map.put(o2.getX(), o2);

现在,如果我想要 x 值为“foo”的对象,那么只要

Object desiredObject = map.get("foo");

顺序很重要,就考虑 LinkedHashMap。

I usually just use a map if i want to be able to fetch an object out of a collection based on one specific attribute value. I find that cleaner than having to iterate over lists.

Map<String, Object> map = new HashMap<String, Object>();

map.put(o1.getX(), o1);
map.put(o2.getX(), o2);

now, if i want the object that has an x-value of "foo", all it takes is

Object desiredObject = map.get("foo");

if order is important, consider a LinkedHashMap.

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