Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 355965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:04:02+00:00 2026-05-12T12:04:02+00:00

I have a 10×10 array in Java, some of the items in array which

  • 0

I have a 10×10 array in Java, some of the items in array which are not used, and I need to traverse through all elements as part of a method. What Would be better to do :

  1. Go through all elements with 2 for loops and check for the nulltype to avoid errors, e.g.

    for(int y=0;y<10;y++){
        for(int x=0;x<10;x++){
           if(array[x][y]!=null)
                //perform task here
        }
    }
    
  2. Or would it be better to keep a list of all the used addresses… Say an arraylist of points?

  3. Something different I haven’t mentioned.

I look forward to any answers 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:04:02+00:00Added an answer on May 12, 2026 at 12:04 pm

    Any solution you try needs to be tested in controlled conditions resembling as much as possible the production conditions. Because of the nature of Java, you need to exercise your code a bit to get reliable performance stats, but I’m sure you know that already.

    This said, there are several things you may try, which I’ve used to optimize my Java code with success (but not on Android JVM)

    for(int y=0;y<10;y++){
        for(int x=0;x<10;x++){
           if(array[x][y]!=null)
                //perform task here
        }
    }
    

    should in any case be reworked into

    for(int x=0;x<10;x++){
        for(int y=0;y<10;y++){
           if(array[x][y]!=null)
                //perform task here
        }
    }
    

    Often you will get performance improvement from caching the row reference. Let as assume the array is of the type Foo[][]:

    for(int x=0;x<10;x++){
        final Foo[] row = array[x];
        for(int y=0;y<10;y++){
           if(row[y]!=null)
                //perform task here
        }
    }
    

    Using final with variables was supposed to help the JVM optimize the code, but I think that modern JIT Java compilers can in many cases figure out on their own whether the variable is changed in the code or not. On the other hand, sometimes this may be more efficient, although takes us definitely into the realm of microoptimizations:

    Foo[] row;
    for(int x=0;x<10;x++){
        row = array[x];
        for(int y=0;y<10;y++){
           if(row[y]!=null)
                //perform task here
        }
    }
    

    If you don’t need to know the element’s indices in order to perform the task on it, you can write this as

    for(final Foo[] row: array){
        for(final Foo elem: row
           if(elem!=null)
                //perform task here
        }
    }
    

    Another thing you may try is to flatten the array and store the elements in Foo[] array, ensuring maximum locality of reference. You have no inner loop to worry about, but you need to do some index arithmetic when referencing particular array elements (as opposed to looping over the whole array). Depending on how often you do it, it may or not be beneficial.

    Since most of the elements will be not-null, keeping them as a sparse array is not beneficial for you, as you lose locality of reference.

    Another problem is the null test. The null test itself doesn’t cost much, but the conditional statement following it does, as you get a branch in the code and lose time on wrong branch predictions. What you can do is to use a “null object”, on which the task will be possible to perform but will amount to a non-op or something equally benign. Depending on the task you want to perform, it may or may not work for you.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 188k
  • Answers 188k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer modelshredder has exactly what you need. If you have the… May 12, 2026 at 5:33 pm
  • Editorial Team
    Editorial Team added an answer The target filesystem has to support the encoding as well… May 12, 2026 at 5:33 pm
  • Editorial Team
    Editorial Team added an answer This problem illuded me for hours, so I'll post this… May 12, 2026 at 5:33 pm

Related Questions

in C#,I have a double variable price with value 10215.24. I want to show
I have a header file in which there is a large struct. I need
I'm currently working in FlexSDK/AS3 and have a canvas containing many objects. The canvas
I have a NAT configured to run when loading up my favorite Linux distribution
I have data that looks like this: entities id name 1 Apple 2 Orange

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.