ArrayList of my objects, indexOf problem

久未见 提交于 2019-12-01 22:48:51

问题


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?


回答1:


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

  • Java Language Guide: the for-each loop
  • java.util.List API

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

  • How to sort an array or ArrayList<Point> ASC first by x and then by y?
  • Java: What is the difference between implementing Comparable and Comparator?



回答2:


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());

}



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/2769797/arraylist-of-my-objects-indexof-problem

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