Alternative to Nested Loop For Comparison

Posted by KGVT on Stack Overflow See other posts from Stack Overflow or by KGVT
Published on 2010-04-23T22:06:46Z Indexed on 2010/04/23 22:23 UTC
Read the original article Hit count: 489

Filed under:
|
|
|

I'm currently writing a program that needs to compare each file in an ArrayList of variable size. Right now, the way I'm doing this is through a nested code loop:

         if(tempList.size()>1){
            for(int i=0;i<=tempList.size()-1;i++)
                //Nested loops.  I should feel dirty?
                for(int j=i+1;j<=tempList.size()-1;j++){
                    //*Gets sorted.
                    System.out.println(checkBytes(tempList.get(i), tempList.get(j)));
                }
            }

I've read a few differing opinions on the necessity of nested loops, and I was wondering if anyone had a more efficient alternative.

At a glance, each comparison is going to need to be done, either way, so the performance should be fairly steady, but I'm moderately convinced there's a cleaner way to do this. Any pointers?

© Stack Overflow or respective owner

Related posts about java

Related posts about loops