Clearing Multidimensional arrays

Posted on 2008-11-10
Medium Priority
Last Modified: 2012-06-27
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?
Question by:billyleo
  • 2
LVL 86

Assisted Solution

CEHJ earned 800 total points
ID: 22920224
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.
LVL 24

Accepted Solution

sciuriware earned 1200 total points
ID: 22920242
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.

LVL 86

Expert Comment

ID: 22920305
Yes, that *could* be faster - time it and see

Author Closing Comment

ID: 31514962
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question