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
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 >

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 >
