user3338991
user3338991

Reputation:

java arrayList loop performance

How can i rewrite this code, to get better performance?

    int i = 0;
    ArrayList<ArrayList> data = new ArrayList<ArrayList>();
    //---- fill data with 2 equally large ArrayLists, 2 table columns here

    for (int n = 0; n < data.get(0).size(); n++) {  //Loop for whole table
       if (i < n){  //not to enter second loop, while in previous second loop
           if (data.get(1).get(n) > 0){  // row with condition when to enter second loop
               for (i = n; i < data.get(0).size(); i++){ //second loop
                  if (data.get(0).get(i) > data.get(0).get(n) + 10 ){ // breaks second loop
                  //somecode
                  break;
                  }
               }
            }
        }
    }

Basically what is does, it goes through table row by row (1st loop), until it finds a first specific "starting" row (where second column is > 0), from this point it looks for another specific "ending" row (2nd loop), until it finds it (value in this row must be at least 10 higher than in starting row) and ends 2nd loop. It will not go into the second loop, if it already is in one. It will look for the starting condition only past the last row with ending condition (the if (i < n) part).

I know it's crude, i made this with highschool visual basic knowledge transformed to java lang.

How can i program this in a better way, to get better performance, as the table/array is really long and i have to go through it a lot of times looking for a different starting condition (perhaps working with db instead of arraylists?, how would a db query look like?)

Upvotes: 2

Views: 508

Answers (1)

AnnoSiedler
AnnoSiedler

Reputation: 273

Personally I woudn't use ArrayList when having just 2 columns.

You could do something like this:

    class Column{
        Integer first;
        Integer second;
    }

    ArrayList<Column> data = new ArrayList<Column>()

But that shouldn't make much difference. By the way: at last tell, which type your ArrayLists are. Like: ArrayList < ArrayList < Integer > > instead of ArrayList < ArrayList > ).

The if "i < n" is quite unnecessary. You could do something like this:

    for (int n = 0; n < data.get(0).size(); n++) {  //Loop for whole table
           if (data.get(1).get(n) > 0){  // row with condition when to enter second loop
               int n0=n;
               for (; n < data.get(0).size(); n++){ //second loop
                 if (data.get(0).get(n) > data.get(0).get(n0) + 10 ){ // breaks second loop
              //somecode
                 break;
                 }
              }
           }
        }

The java for-loop is really flexible. Java just calls first "parameter" before calling the loop, and last "parameter" after each iteration. And the loop continues till second "parameter" returns False.

So you don't have to skip iterations with if - just increment n and this will also skip loop iterations. If you e. g. set n=10, then java will continue calling the loop iteration for n=10, 11, 12, and so on. If you just also use n as your variabel counter for the second loop, it will do, what you want.

Using database is quite unnecessary.

I don't know exectely what you want to do. Sorting your table before calling your code might open completely new options. If you don't want to sort the table, just iterating element by element should be the only possibility.

Upvotes: 1

Related Questions