ArrayList of my objects, indexOf problem

后端 未结 4 1494
一个人的身影
一个人的身影 2021-01-20 12:04

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

4条回答
  •  抹茶落季
    2021-01-20 12:50

    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.

提交回复
热议问题