allocate array elements one at a time?

Is there a way to allocate array elements one at a time?

The following code will crash because the minimumLogIndices array is not allocated.

// get all indices where cArray[i] > minimumLog

int[] minimumLogIndices =null;

for (int i=0; i < columns; i++)

{

if ((cArray[i] > minimumLog))

{

minimumLogIndices[i] = i;

}

}

I would like a statement like:

minimumLogIndices[i] =new(i);

but this is not allowed, of course.

If I use ArrayList, then the elements can be added one at a time, but I cannot use int.

null

[949 byte] By [allelopatha] at [2007-11-26 20:49:31]
# 1

int[] minimumLogIndices = new int[columns];

As described here:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

[Edit] The array will, however, have "holes" in it when cArray <= minimumLog, but that's inevitable if you are using an array.

pbrockway2a at 2007-7-10 2:13:29 > top of Java-index,Java Essentials,Java Programming...
# 2
If you don't want the overhead of boxing your integers, then there are primitive implementations similar to ArrayList<Integer>, such as http://jakarta.apache.org/commons/primitives/Pete
pm_kirkhama at 2007-7-10 2:13:29 > top of Java-index,Java Essentials,Java Programming...
# 3
Pete,Thanks for pointing this out, I was not aware of itAl
allelopatha at 2007-7-10 2:13:29 > top of Java-index,Java Essentials,Java Programming...