开发者

Need help using an Iterator

I'm completely new to iterators. I have an ArrayList named "Test" with String objects. How would I go about using an iterator class? I've tried everything I can think of, it's just not making sense. Thanks for the help.

Say I have an Iterator named "iter". I need to step through my ArrayList in search of a certain String. When that String is found, I need to add it to a different ArrayList named "test2".

while(iter.hasNext()) {
    if(iter.next() == sampleString) {
        test2.add(sampleString);
    }
}

Only problem with this, is that 开发者_运维问答when I call next() it moves the pointer to the next String, ignoring the first String in the ArrayList. How would I implement this??


You don't need one. The ArrayList is already Iterable! :-D

ArrayList<String> test = new ArrayList<String>();
test.add("Hello");
test.add("world");
for(String str : test) System.out.println(str);


Iterators are generally used like this:

while (iter.hasNext()) {
    String nextString = iter.next();
    // Do something with the string...
}

Some (myself included) will prefer the enhanced for loop:

for (String nextString : listOfStrings) {
    // Do something with the string
}

The for loop avoids the need for getting an explicit Iterator reference and includes the nextString variable declaration, keeping it concise and properly scoped.


The issue is that your not quite 100% on how Iterator.next() works

This is copied directly from the Java API

E next() -- Returns the next element in the iteration.

What this means is that .next() will return an object then move to the next item in the list.

You need to store the returned object when calling next()

while(iter.hasNext()) 
{
    String temp = iter.next();
    //this is a more old school method but more true to form. 
    //== doesn't necessarily do equality checks on all objects the way
    //you would think 
    if(temp.equals(sampleString)) 
    {
        test2.add(temp);
    }
}


An iterator is simply something that understands how to traverse a given data structure.

What is it that you aren't understanding?

For a List an iterator would keep track of its current position in the list and understand how to get the next element in the list. For something like a list, it's relatively simple, but you could also define an iterator for some other arbitrary data structure that isn't as simple as a list. You could also define one that does something differently like traverse the list backwards. If you have specific questions about iterators, update your question and we'll take a stab at it :-)


Iterator is a means of traversing a collection of items. Adhering to that statement, the java.util.Collection extends Iterable (public interface Collection<E> extends Iterable<E>). i.e all collection classes are Iterable in someway. iterator() is the method to get a handle to that Iterator. Once you have the handle you can traverse through the items.

I highlighted someway because not all Iterator's allow bi-directional traversal. ListIterator allows this.

I'd rewrite your code as below

for(String s : myCollection)
{
   if(s.equals(sampleString))
   {
      test.add(s);
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜