Clearing Multidimensional arrays

Hi all,

I m hoping somebody can tell me which is the quickest way (in app performance) to clear multi dimensional arrays?

These arrays have up to 10 dimensions and each dimension has approx 8 indexes. The obvious way to clear (by clear i mean set to default value, in this case 0 because the arrays are doubles) is to run a few nested loops but doing this adds far too much time to the app. i also tried setting the object hold the arrays equal to a another object reference that had just been instantiated but this actually added even more time.

My final idea is to use an arraylist as the first dimension of the array andcalling .clear() on this. however this would require alot of rewriting of code so i havent tried it yet.

Any suggestions?
Who is Participating?
sciuriwareConnect With a Mentor Commented:
The quickest way of clearing an array of doubles is ........................ allocating a new array.

double[][] ddd = new double[12][45];  // Will contain 12 * 45   0.0 values.

CEHJConnect With a Mentor Commented:
There's no quicker way to do it. Unfortunately Java has no direct memory access, so you can't zero out a block of memory.

You can reduce the amount of code you need to write by calling Arrays.fill

If you're already using multi-dimensional arrays, simply iterating them to zero out the memory is unlikely to be the main cause of performance problems.
Yes, that *could* be faster - time it and see
billyleoAuthor Commented:
Thanks to both of you for answering - i think i'll go with allocating a new array, i tried .fill but got all confused with the syntax
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.