unknown array size

I have a function that searches through an ArrayList and sees if one of the objects' variable has a certain value. What I want to do is return the index of all of the objects with a certain values. But because I don't know how many objects there are with that certain value before hand I can't just place the indexes in an array. Should I just use an Arraylist, or is there some other way around this?

Thanks

[424 byte] By [java4life87a] at [2007-10-3 3:30:10]
# 1
use ArrayList and then, if you want, convert it to standard array:return (Integer[])yourArrayList.toArray(new Integer[0]);
lfschucka at 2007-7-14 21:24:01 > top of Java-index,Java Essentials,Java Programming...
# 2
Is that more efficient than just returning the ArrayList?
java4life87a at 2007-7-14 21:24:01 > top of Java-index,Java Essentials,Java Programming...
# 3

Just returning the List (or maybe a Set) of the indexes is probably the easiest. In any case, if you need the array, allocate the array to the correct size (instead of using zero):

return (Integer[])yourArrayList.toArray(new Integer[yourArrayList.size()]);

MLRona at 2007-7-14 21:24:01 > top of Java-index,Java Essentials,Java Programming...
# 4
you can be sure that it's extra CPU time... and a matter of return type and personal taste ;-)but you have no much choice: use a dynamic collection to store the ints or count them first and create an standard array with that size (pretty bad idea)
lfschucka at 2007-7-14 21:24:01 > top of Java-index,Java Essentials,Java Programming...
# 5

Depending on what you want to do with the indexes, you could also make an ArrayList with references to the actual objects, instead of storing the indexes. As you probably know, if you need to put the indexes in a collection (List, Set), you will need Integer objects, not 'int' primitives (Java 1.5 autoboxing may hide the difference somewhat, but you still end up with Integer objects).

MLRona at 2007-7-14 21:24:01 > top of Java-index,Java Essentials,Java Programming...