Sylvester Peter Sylvester Peter -4 years ago 89
Java Question

Very minor code optimisation

while (i < a.size() && value2.substring(0, prefix.length()).compareTo(prefix) == 0) {
value2 = a.get(i);

if (value2.endsWith(suffix)) {
counter++;
setter = true;

}

i++;

}


I was just wondering if there was a way to avoid using the get() method twice in my code. My problem right now is that I need to assign value 2 before so that my while loop works but I also need to update it within the while loop.

Answer Source
        while(i <a.size()){
            value2 = a.get(i);
            if(value2.substring(0,prefix.length()).compareTo(prefix)!=0){
                break;
            }
            if(value2.endsWith(suffix)){
                counter++;
                setter = true;

            }
            i++;
       }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download