Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Searching and printing string results from an Arraylist. Having difficulty.

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Searching and printing string results from an Arraylist. Having difficulty.

    Hi, been having trouble with the last two methods of an assignment.

    Basically I need to search an arraylist for a string that matches for example, 'waterford' and print to screen each of these results.

    Also we need to print to screen an address, based on a name being entered.

    What would be the best way to implement this, and would HashMap or HashSet be better?
    We've been thinking along the lines of this

     
    public void searchByCounty()
            {
                int index =0;
                boolean found = false;
                while(index < addressLine.size() && !found)
                    {
                        String county = nam.get(index);
                        if(Address.contains(searchString))
                            {
                                found = true;
                            }
                        else
                            {
                                index++;
                            }
                    }
                }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Searching and printing string results from an Arraylist. Having difficulty.

    If you do not need multiple entries or proper ordering, a HashSet would work. Although the Hashset is founded upon a HashMap, if you don't need key/value pairs you can easily go with HashSet. If you want to link up locations with multiple hashsets (such as key a HashMap with street -> city, and city->county, and count->country, etc...), HashMap would work well. One more note, if you place custom written objects in the Map or Set you need to override and implement the hashCode() and equals() methods.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Espressoul (February 25th, 2010)

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. searching a string
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 12:31 AM
  3. How to print results to screen
    By NinjaLink in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2010, 12:46 PM
  4. Football Results
    By RSYR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2009, 06:24 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM