Results 1 to 3 of 3
  1. #1
    oldmanhouse is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default Newbie help with ArrayList!

    Hello all

    I have set up an ArrayList to store objects and I want to create a method to search through the ArrayList and return the number of times a single String object (specified in the method header parameter).

    For example:

    Array1:
    Bill, Jim, Bill, Ted, Bill
    Method returns: 3

    The method would search through the array, see "Bill" 3 times and return the integer 1 indicating that "Bill" appears 3 times in the Array.

    I am making sense? I'm clueless here. I know what I want to accomplish, but when it gets down to the coding i'm completely stumped!

    As a starting point, I have...

    public int countNames(String name)


    Thanks :)

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    try something like
    Java Code:
    if Array1[x].contains(String object from parameter){
             count++;
    }
    and just put a loop around it from 0-array1.size();

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    see "Bill" 3 times and return the integer 1
    This part doesn't make sense. What is returned if found 2 times or 5 times?
    The basic logic is to use a loop,
    In the loop get the next item in the ArrayList (read the API doc to see which method to use to get the next item)
    Compare the item to the searched for Object. Read the API doc for the type of Object to see how to test its contents. For example equals() could be used for Strings)
    Use an if() with the above compare, when true increment the count.
    When there are no more items in the ArrayList, end the loop.

Similar Threads

  1. :) newbie...........
    By Somitesh Chakraborty in forum Introductions
    Replies: 1
    Last Post: 08-19-2008, 09:00 AM
  2. newbie newbie newbie
    By krislogy in forum New To Java
    Replies: 9
    Last Post: 08-15-2008, 12:28 AM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. newbie needs help...
    By vicky08 in forum New To Java
    Replies: 2
    Last Post: 03-31-2008, 04:26 PM
  5. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •