The Artima Developer Community
Sponsored Link

Java Answers Forum
iterators

4 replies on 1 page. Most recent reply: Apr 14, 2003 12:20 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Duane

Posts: 5
Nickname: duane
Registered: Mar, 2003

iterators Posted: Apr 13, 2003 6:35 AM
Reply to this message Reply
Advertisement
I need a method that will use a list iterator to go through a list and search for a matching string. Any ideas would be helpful.
Thanks


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: iterators Posted: Apr 13, 2003 8:21 PM
Reply to this message Reply
> I need a method that will use a list iterator to go
> through a list and search for a matching string. Any ideas
> would be helpful.
> Thanks

LinkedList _latestEntries;
 
//add some data to the linked list...
//.
//.
//.
//
 
ListIterator listIter = _latestEntries.listIterator(0);
 
while (listIter.hasNext()){
    String entry = (String)listIter.next();
 
    //text to search for
 
    if (entry.indexOf(text)!= -1) {
          System.err.println("Found "+text+" in " + entry);
    }
}

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: iterators Posted: Apr 14, 2003 3:09 AM
Reply to this message Reply
A more general version

public void search( List list, String stringToFind )
{
    // check the string is in the list
    if( list.contains( stringToFind ) )
    {
        // string is in list
        Iterator iterator = list.iterator();
        while( iterator.hasNext() )
        {
            String s = (String) iterator.next();
            if( s.equals( stringToFind ) )
            {
                // found the string...
            }
        }
    }
    else
    {
        // string is not in list
    }
}


Adam

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: iterators Posted: Apr 14, 2003 12:18 PM
Reply to this message Reply
The specification isn't very clear. For instance, if the collection contains the string "'twas brillig and the slithy toves did gyre and gimble in the wabe" and you search for "brillig", should it find it in that string or should it only find exact matches? And should it find matches in values only, or also in keys? And what about case-sensitivity?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: iterators Posted: Apr 14, 2003 12:20 PM
Reply to this message Reply
Disregard the keys/values comment; I was thinking this was a Map (from a previous question). Since this is just a list, only values are relevant.

Flat View: This topic has 4 replies on 1 page
Topic: Java Editor... Previous Topic   Next Topic Topic: pi/4 =??

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use