File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Using compareTo in a Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using compareTo in a Loop" Watch "Using compareTo in a Loop" New topic
Author

Using compareTo in a Loop

Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
I'm trying to use compareTo to walk through an ArrayList, but it's not working. Here's my code with the errors following:

Ch10Ex10.java:33: cannot find symbol
symbol : variable compareTo
location: class java.lang.String
if(list.get(i).compareTo.first > 0 & list.get(i).compareTo.end < 0){

^
Ch10Ex10.java:33: cannot find symbol
symbol : variable compareTo
location: class java.lang.String
if(list.get(i).compareTo.first > 0 & list.get(i).compareTo.end < 0){
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Ok, got it. Here is the proper syntax. I'm still testing it to see if it's doing what I want, which is to remove all words from the list that fall between the first word that I give it from outside the list and the last word I give it from outside the list. It needs to leave all the other works in the list undisturbed.
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Apparently the syntax is correct but the comparison is not working for some reason that I cannot figure out. Still needing some help with this one.

Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, I can point out three things here; two of them are going to make the code work incorrectly, one is an efficiency thing.

1) The Java idiom for a "for" loop over a list is:

for (int counter=0; counter < list.size(); ++counter)

If the list is 10 items long, counter will take the ten values from 0 to 9; it will stop before counter is 10 because of the "<" operator. Get it? The "size()-1" is unnecessary, and means that the loop will skip the last element, which is probably not what you want.

2) Say you're iterating over a list, and you find that get(3) returns an element that you want to remove. So you say remove(3). The element that was at index 4 moves down into slot number 3. Your loop then looks at the element in slot number 4, which is the one that was originally in slot number 5 -- the loop never looks at the element that was originally in slot number 4! Again, this is probably not what you want. One way to fix this is to iterate backwards:

for (int i=list.size()-1; i>= 0; --i)

This loop starts at the last element and runs until element 0 is processed. The elements that get shifted down to fill the "holes" are elements you've already looked at.

3) You've used "&" in the "if" test instead of "&&". You should virtually always use "&&", which skips the second test if the first one fails -- that's obviously more efficient. Use "&" only if your tests have some side effects that you don't want to skip (which would be bad programming practice, in any event.)


[Jess in Action][AskingGoodQuestions]
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Ernest, yes that fixed it. I did find the first problem in my code before your post and understand what you're saying. Also, the single & problem, too. I understand that better now, too.

It did not occur to me about the list elements shifting when you remove one, even though that's in our textbook and was explained by our teacher. Then, as for the fix, I don't know if I ever would have thought of going in reverse.

Thank you very much,
Dave
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
One more question on this. Does case make any difference in the comparison? If so, then I will have to put in some ignoreCase in the appropriate places.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Dave DiRito:
One more question on this. Does case make any difference in the comparison? If so, then I will have to put in some ignoreCase in the appropriate places.


Well, it's your program, so I can't tell you if letter case matters or not! But I can tell you that compareTo() will sort all upper case English letters before all lower case ones, and that "FOO" and "foo" will not be equal. If you want "FOO" and "foo" to be the same, and "ZOO" to come after "apple", then using compareToIgnoreCase() would be good.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Originally posted by Ernest Friedman-Hill:
2) Say you're iterating over a list, and you find that get(3) returns an element that you want to remove. So you say remove(3). The element that was at index 4 moves down into slot number 3. Your loop then looks at the element in slot number 4, which is the one that was originally in slot number 5 -- the loop never looks at the element that was originally in slot number 4! Again, this is probably not what you want. One way to fix this is to iterate backwards:

for (int i=list.size()-1; i>= 0; --i)

This loop starts at the last element and runs until element 0 is processed. The elements that get shifted down to fill the "holes" are elements you've already looked at.

This can be solved by using an iterator:

Two rules about using iterators:
1) ALWAYS call i.next(), preferably as the first call in the loop
2) NEVER call i.next() multiple times in the loop, unless checking i.hasNext() in between


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Thank you, Ernest and Rob. Your comments tremendously help my understanding.

Dave
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Originally posted by Rob Prime:

This can be solved by using an iterator:
i.remove(); // removes the element while still keeping the order
I had forgotten about the remove() method of the Iterator; that is why I gave misleading advice not to use an Iterator earlier.

I am very sorry.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Don't be. Your code can be used for any case where there is no iterator. I use it a lot with table models and list models when removing.
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
No problem, Campbell. I couldn't have used an iterator solution for this problem even if you had suggested it because we haven't studied iterators yet in the course I'm taking. We can't use anything we haven't covered yet.

I appreciate the iterator preview anyway.

Thanks, Rob and Campbell,
Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using compareTo in a Loop