0

Suppose I have a custom object set up in a class similar to this.

public class anObject {

  public String id, otherProperty;

    public anObject(){
       this.id = "1";
       this.otherProperty = "cat";
    }
}

Then I create an array of these objects in another class

anObject[] objects = new anObject[40];
for(int i=0; i < 40; i++){
    objects[i] = new anObject();
}

What can I do then to find the first object in the array that has an id of 2 (for example)?

5 Answers 5

2
anObject found = null;

for(int i=0; i < 40; i++){
  if ("2".equals(object[i].id)) {
    // found it
    found = object[i];
    break; // exit the loop
  }
}

Or am I missing something?

EDIT: added the break. Also, there is a convention that class names begin with an uppercase letter, such as AnObject.

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

3 Comments

You're just missing that id is a String ;)
I knew it was something so simple, but this code will find all the objects with an id of 2 won't it? How do I cancel the loop so it only gets the first one?
Or use a boolean and a while... Well pretty much a lot of solutions.
1

There are multiple ways of going about this. First, you could do a simple for loop, iterating over all of the objects until one with a specific id is found. Your search complexity would be O(N)

anObject obj = null;
dance: for( int i = 0; i < objects.length; i++ )
{
    if( object[i].id == 2 )
    {
         obj = object[i];
         break dance;
    }
}

if you know that you're always going to be searching by id, you could implement Comparable. Then you can use java.util.Arrays to sort and search the array for you. This reduces your search to O(log n)

public class anObject implements Comparable {

  public String id, otherProperty;

    public anObject(){
       this.id = "1";
       this.otherProperty = "cat";
    }

    public int compareTo( Object o )
    {
         if( o instanceof anObject )
         {
             return this.id.compareTo( ( (anObject) other).id );
         }
         return -1;
    }
}

Last option, you can store the results in a Map<String, anObject>. If you're doing a lot of searching, this is the best method as it gives your search O(1), at the cost of additional memory.

Comments

0

There's no other way besides iterating through them and checking manually, as Matthew showed you. You can store them in the order of the id and do something like binary search to cut down time to O(log(n)) instead of O(n), but that might be too much overhead.

You can try storing them in a Map<String, YourObject> and just do map.get(id). This has O(1) access time.

Map<String, YourObject> map = new HashMap<String, YourObject>();
for (int i=0; i < 40; i++) {
    YourObject obj = new YourObject(); // couldn't put anObject, it bugged me :)
    map.put(obj.id, obj);
}

// get object with id = 2
YourObject secondOne = map.get("2");
if (secondOne != null) {
    ...
}

The best way to do this depends your main use-cases, really, and on how many elements you plan on supporting.

Comments

0
public static anObject findById(anObject[] arr,String str) {
 for (anObject obj:arr) if (obj.id.equals(str)) return obj;
 return null;
}

And then call anObject.findById(objects,"2")

Comments

0

Use Commons Collections: http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html#find(java.util.Collection, org.apache.commons.collections.Predicate)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.