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?)
ArrayList<ArrayList<Integer>>) and the fact you have two columns and they are anonymous doesn't help the readability. You can make two 1D lists, you can also make a 1D list with some custom object of yours as an entry, it will help you make it more readable for both you and us.