-3

I have an array of Objects. Now my question is how I can find out what position the object is in from within it?

My object array

public myObject[] objects = new myObject[10];

The constructor of myObject

public myObject(){
    int objectID = 0; //Here should be the code to find the position in the array
}
2
  • Try something first, then we will help you. Commented Jul 19, 2013 at 14:42
  • How could the object be in an array if it's not even constructed yet? Show us real code. Your code doesn't make much sense as is. And respect the Java naming conventions. Commented Jul 19, 2013 at 14:43

2 Answers 2

3

Loop over your array to find the position of your object, using whatever equality check you require.

EDIT: As everyone seems to be concerned over .equals vs == I've changed the routine to perform either. If you're checking for reference equality, use findValue(ary, obj, true), otherwise use findValue(ary, obj, false)

This will not find null values. I suggest throwing an exception here if myObject is null, but I will leave that decision to you.

public int findValue(Object[] objects, Object myObject, boolean equalityCheck){
    for(int i=0;i<objects.length;i++){
        if(equalityCheck){
            if(objects[i] == myObject){
                return i;
            }
        }else{
            if(objects[i]!=null && myObject!=null && objects[i].equals(myObject)){
                return i;
            }
        }
    }
    return -1;
}

This code is performing a linear search to find the position of the object. Its not very efficient on larger arrays. As you seem to be a beginner I'm not going to dive into how to optimize this, but just be aware.

Sign up to request clarification or add additional context in comments.

15 Comments

-1 objects == myObject will never work in this case (but looks that upvoters don't even understand the problem with this approach).
Syntax error, fixed. Small mistake.
@WilliamMorrison == compares references, so in the first moment you use new ... you have a totally new reference. Now with your edit, it's not easy to see if you're indeed passing an existent element of the array to this method. When comparing object references, you should use equals instead of ==.
@WilliamMorrison As Luiggi mentioned, you should replace == with .equals in your code. == will always yields false in this case.
Ah my mistake you are both correct. The code has changed many times since your comments however, I hope you understand my confusion. No need for personal attacks, they show insecurity on your part.
|
1

Your class MyObject should have a method which gets the array and returns the idx

class MyObject {
   public int findIdx(MyObject[] myObjects) {
        if (myObjects != null) {
            for (int i = 0; i <= myObjects.length; i++) {
                if (myObjects[i].equals(this)) {
                    return i;
                }
            }
        }
        return -1;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.