Efficient loop code? Also, a query about the "for each" loop

user2911290

The following code compiles and does what I require. It iterates through an int-based multidimensional array (called nums), and searches for all occurances of the value 1. This program's output is shown below. There are three things to note:

  • Regarding the "outer for" loop statement, I have used Java's comma operator to declare two additional variables.
  • Also regarding this "outer for", I've used another comma operator in the "iteration section", to reset one of these additional variables.
  • Regarding the "inner for", I've used another comma operator in the "iteration section" to increment this additional variable.
int[][] nums = {{1,1,2},{3,4},{5},{6,7,8},{9,1,1}};

for (int i=0, elsSearched=0, foundCount=0; i < nums.length; i++, elsSearched=0) {
    for (int j=0; j < nums[i].length; j++, elsSearched++) {
        if (nums[i][j] == 1) {
            foundCount++;
        }
    }
    System.out.println("Searched " + elsSearched + 
" elements, and found \'number one\' a total of " + foundCount + " times..");
}       

Program output:

Array search results

Could this code be written more effeciently/elegantly?

My other query is about Java's "for each" loop. I tried rewritting the code above using the "for each" loop with the comma operator, but the code wouldn't compile. I quickly came to the obvious conclusion, but would the "for each" loop be better if the comma operator could be used with it, or would that just introduce "distracting clutter"?

jmiserez

Edit: Be aware that foundCount is the total number of elements found up until now, as it is never reset to zero. Was this intended?

Don't you agree that this code is easier to read and more concise? (Note: efficiency is the same as your code)

int[][] nums = {{1,1,2},{3,4},{5},{6,7,8},{9,1,1}};

int foundCount = 0; 
for(int[] inner : nums){
    for(int i : inner){
        if (i == 1){
            foundCount++;
        }
    }
    System.out.println("Searched " + inner.length + 
                " elements, and found \'number one\' a total of " + foundCount + " times..");
}

Output:

Searched 3 elements, and found 'number one' a total of 2 times..
Searched 2 elements, and found 'number one' a total of 2 times..
Searched 1 elements, and found 'number one' a total of 2 times..
Searched 3 elements, and found 'number one' a total of 2 times..
Searched 3 elements, and found 'number one' a total of 4 times..

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Efficient loop code? Also, a query about the "for each" loop

From Dev

Python Sqlalchemy: Efficient Query and For Loop

From Dev

SQL - For Each loop in a query

From Dev

How to avoid doing a query each time loop runs? (optimize code)

From Dev

Replace This While Loop with More Efficient Code?

From Dev

Question about simplifying code related to loop iteration

From Dev

Running a loop for each mysql query result fields

From Dev

Trying to use a Query each loop on Raphael object

From Dev

Loop through CTE results and run a query for each

From Dev

My loop code on jquery doesn't work with .each inside the loop

From Dev

vbscript for each loop to for loop

From Dev

jQuery loop / .each function to simplify code

From Dev

VBA For Each Loop to Excel JavaScript API Code

From Dev

Ruby On Rails, code in each loop in view

From Dev

jQuery loop / .each function to simplify code

From Dev

Parse cloud code delay each iteraton of for loop

From Dev

How can one improve this code with a loop so that it is much shorter and efficient?

From Dev

Efficient alternative to for loop of ifelse

From Dev

Energy efficient spin loop

From Dev

efficient loop in R

From Dev

Efficient loop multithreading

From Dev

efficient vectorizisation of a double for loop

From Dev

JOGL efficient render loop

From Dev

Making loop more efficient

From Dev

Efficient alternative to for loop of ifelse

From Dev

Making loop more efficient

From Dev

React Native Set State not a function, in a for each loop not sure about binding?

From Dev

I have a question about the while loop in my code

From Dev

regexp loop to find first instance of each query TCL

Related Related

HotTag

Archive