Clearing Multidimensional arrays

Posted on 2008-11-10
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
    LVL 86

    Assisted Solution

    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

    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

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

    Author Closing Comment

    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

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    java constructor error 8 66
    countHi challenge 25 63
    endX challenge 2 33
    What is JNDI datasource in spring 1 21
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now