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 6 of 6

Thread: newby Questions - indexOf help with an ArrayList

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default newby Questions - indexOf help with an ArrayList

    I'm trying to build an address book application, but before I start I'm familliaring my self with arrayLists of objects.

    I have two classes (and a third tester class):
    DirectoryEntry
    PhoneDirectory

    DirectoryEntry has a constructor that takes in two strings as arguments and saves then in class variables, name and number.

    (for now) all PhoneDirectory does is ceate an arraylist:

    dir = new ArrayList<DirectoryEntry>

    and has a method to add an entry to the array list.

    I want to us the indexOf("Stephen") to return an index number in the array, right now all I'm getting is -1.
    What bugs me is that if I put something like:

    DirectoryEntry name = dir.get(1);

    I get the name and number in slot 4. I'm just not understanding the indexOf() and OOP : S please any help would be most welcome


    public class Driver
    {
     
            public static void main(String[] args)
            {
                PhoneDirectory phone = new PhoneDirectory();
                phone.addEntry("Stephen", "447-8067");
                phone.addEntry("Vickie", "415-8006");
                phone.addEntry("Jake", "675-8064");
                phone.addEntry("Jen", "879-5684");
                phone.addEntry("Anna", "710-4564");
     
                phone.displayContents();
     
            }
     
     
     
    }

    import java.util.ArrayList;
     
     
    public class PhoneDirectory {
     
        public ArrayList<DirectoryEntry> dir;
     
        public PhoneDirectory()
        {
            dir = new ArrayList<DirectoryEntry>();
        }
     
        public void addEntry(String name, String number)
        {
            dir.add(new DirectoryEntry(name,number));
     
        }
     
        public void displayContents()
        {
     
           // DirectoryEntry aName = dir.get(3);
            int ind = dir.indexOf("Stephen");
     
            System.out.println(ind);
        }
    }

    public class DirectoryEntry {
        private String name;
        private String number;
     
        public DirectoryEntry(String name, String number)
        {
            this.name = name;
            this.number = number;
        }
     
        public String name()
        {
            return name;
        }
     
    @Override
         public String toString()
        {
            return name + " " + number;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: newby Questions - indexOf help with an ArrayList

    I want to us the indexOf("Stephen") to return an index number in the array,
    Is the String "Stephen" an object in the ArrayList?
    Or is it inside of another object, a DirectoryEntry?
    It looks like the ArrayList contains DirectoryEntry objects, not Strings.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: newby Questions - indexOf help with an ArrayList

    What Norm said.
    Also:
    DirectoryEntry name = dir.get(1); I get the name and number in slot 4
    Call my cynical, but I absolutely doubt that It's returning the 4th ArrayList entry based on the code you posted.

    Edit: To add to Norms post, check out indexOf and read the notes under 'returns'.
    Last edited by newbie; September 20th, 2011 at 05:48 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: newby Questions - indexOf help with an ArrayList

    I won't call you cynical but I will call you cautious...Sorry, the actual code was:
    do
    DirectoryEntry aName = dir.get(3); < - This actually does work, as you can see I'm using get on a DirectoryEntry object - I just don't know how to do something like this to search and get an index number. if I do this:

    DirectoryEntry aName = dir.indexOf("Stephen"); I get bupkis

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: newby Questions - indexOf help with an ArrayList

    DirectoryEntry aName = dir.indexOf("Stephen"); I get bupkis
    To find the element in the list with that contents you will need a loop that looks at each element in the list and tests if it contains the contents you want.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: newby Questions - indexOf help with an ArrayList

    DirectoryEntry aName = dir.get(3);
    The first index in an ArrayList starts at 0.
    So when you say get(3), you're actually referring to the 4th entry.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Array problems...newby...confused?
    By toppcon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 9th, 2011, 03:10 AM
  2. Newby Question
    By javabob in forum Java Theory & Questions
    Replies: 1
    Last Post: November 16th, 2009, 10:09 AM
  3. contains vs. indexOf
    By mgrootsch in forum Java Theory & Questions
    Replies: 5
    Last Post: September 17th, 2009, 08:09 AM
  4. [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
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM