Hello , I have a List<String> and I want to compare in a loop the elements.

For example sth like :

for (int i=0;i<theList.size();i++){
            while (theList.get(i).equals(theList.get(i+1)))
            ...

        }

I want to compare the next to the previous entry.

Thanks

1) If I start the loop at 1 ,do I have to do "i<=theList.size()" ?

2) Actually ,I want to :

while ( !  theList.get(i).equals(theList.get(i+1)))
System.out.println(theList.get(i));

1) No. It's fine the way it is now. The problem with your code is that it will go out of bounds on the last entry. That's why you should start from 1 and compare i to i-1. Or do what you have and end when i < theList.size() - 1

2) That should work just fine.

Ok,thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.