aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList with Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList with Iterator" Watch "ArrayList with Iterator" New topic
Author

ArrayList with Iterator

Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
This method should take a name that the user provides and insert it into a sorted ArrayList in the correct location. I have a syntax problem that I cannot solve. Here's the code:

Thanks,
Dave
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

There is no method called add that takes an Iterator object. It can only accept an integer for the index.


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
Thanks, Rob. I was wondering about that.

So how can I tell where the iterator is at? How do I get the index at that particular point?
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Ok, figured that part out, but now I have another, similar problem. I'm trying to get it to compare the name to be inserted to the word that's at the index of where the iterator is at that point in the loop.

Here's my method now with the compile error that I get:


AddName.java:36: cannot find symbol
symbol : method get(java.lang.String)
location: class java.util.ArrayList<java.lang.String>
}else if(name.compareToIgnoreCase(list.get(itr.next())) >= 0 ){
^
1 error
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Ok, fixed that. Now it compiles, but does not insert the new word in the proper order, so I have a logic error now. Here's the code now:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
I am surprised that runs at all; you usually get an Exception if you try to add or remove (other than using the Iterator#remove() method) while there is an Iterator in scope.
Dave DiRito
Ranch Hand

Joined: Feb 08, 2008
Posts: 77
Don't know why it runs, Campbell. Anyway, because it didn't do what I wanted, I changed the code to the following. This compiles and inserts the word in the proper spot, except if the word comes after the last word in the list. This might work with a LinkedList, but not an ArrayList. Any suggestions?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList with Iterator