-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
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Dirk
  • 125
  • 1
  • 4
  • 12

2 Answers2

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.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • 2
    -1 `objects == myObject` will **never** work in this case (but looks that upvoters don't even understand the problem with this approach). – Luiggi Mendoza Jul 19 '13 at 14:44
  • Syntax error, fixed. Small mistake. – William Morrison Jul 19 '13 at 14:44
  • I'm missing a `break` when you found your object. Why wasting time? – cr0 Jul 19 '13 at 14:46
  • @MarounMaroun 1st: we must not fix answerers code, just help with format. 2nd: using `==` is a **huge** mistake here. – Luiggi Mendoza Jul 19 '13 at 14:47
  • I disagree. Depends on what you're trying to do. – William Morrison Jul 19 '13 at 14:48
  • 1
    @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 `==`. – Luiggi Mendoza Jul 19 '13 at 14:50
  • 1
    @WilliamMorrison As Luiggi mentioned, you should replace `==` with `.equals` in your code. `==` will **always** yields false in this case. – Maroun Jul 19 '13 at 14:51
  • By your last edit, looks like you really need to learn Java... – Luiggi Mendoza Jul 19 '13 at 14:52
  • 1
    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. – William Morrison Jul 19 '13 at 14:52
  • @WilliamMorrison your code show insecurity about your knowledge... – Luiggi Mendoza Jul 19 '13 at 14:53
  • The method I wrote above is used in a framework I work with daily. :) @LuiggiMendoza – William Morrison Jul 19 '13 at 14:54
  • Thanks for noticing me, I won't use that framework :) – Luiggi Mendoza Jul 19 '13 at 14:55
  • Your loss. Couldn't help but notice. Your blather is incessant. – William Morrison Jul 19 '13 at 14:57
  • Instead of writing such bad code for equality check, use something from a real framework like [`ObjectUtils#equals`](http://www.jarvana.com/jarvana/view/com/alibaba/citrus/tool/antx-autoexpand/1.0.10/antx-autoexpand-1.0.10-sources.jar!/org/apache/commons/lang/ObjectUtils.java?format=ok) (check that code, that's a real world framework). – Luiggi Mendoza Jul 19 '13 at 14:59
  • uh huh, looks like something a first year student would learn. Perfect for you! http://www.java2s.com/Open-Source/Android/Game/libgdx/com/badlogic/gdx/utils/Array.java.htm – William Morrison Jul 19 '13 at 15:03
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;
    }
}
Flo
  • 411
  • 4
  • 11