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

Thread: Constructors, Hash Tables, & Linked Lists

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Constructors, Hash Tables, & Linked Lists

     
    Hi Everyone. I'm in the middle coding something and I hit a wall and don't know how to get past it. What I'm trying to do is the following:

    I have a constructor of name "WordDetails" and I have several methods in it like "AddPage", "AddName", and "PrintName". I made a linked list of type "WordDetails" and created 1 link in the list. I added the 1st element in the list into a hash table using the string "One" as the key and the 0th element in the list as the value being added to the table. I need to know how access it so I can run any method from it from the hash table. Here is my code:

    //[I]The Constructor[/I]
    //-------------------------------------------------------------------------------------------------------------------
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    public class WordDetails
    {
        public String WordName;
        public LinkedList <Integer>PageNumbers = new LinkedList<Integer>();
     
        public WordDetails()
        {
        }
     
        //Checks and eliminates same numbers in the list
        public void CheckNumbers()
        {
            if(PageNumbers.size() > 1)
            {
                for(int i = 0; i < (PageNumbers.size() - 1); i++)
                {
                    if(PageNumbers.get(i) == PageNumbers.get(i+1))
                    {
                        PageNumbers.remove(i+1);
                    }
                }
            }
        }
     
        public void AddPage(int x)
        {
            PageNumbers.add(x);
            CheckNumbers();
        }
     
        public void AddName(String Word)
        {
            WordName = Word;
        }
     
        public void PrintName()
        {
            System.out.println(WordName);
        }
    }
    //-------------------------------------------------------------------------------------------------------------------
     
     
    //[I]The Main Class[/I]
    //-------------------------------------------------------------------------------------------------------------------
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    public class IndexForABook
    {
        public static void main (String args[]) throws Exception
        {
            Hashtable WordList = new Hashtable();
            LinkedList <WordDetails>TempList = new LinkedList<WordDetails>();
            TempList.add(new WordDetails());
            WordList.put("One",TempList.get(0));
            //[B][I]Here is where I would need to know how to access methods in the instance of the[/I][/B]
            //[B][I] constructor I added to the hash table[/I][/B]
    }
    //-------------------------------------------------------------------------------------------------------------------
    Last edited by illusion887; December 1st, 2009 at 11:40 AM.


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

    Default Re: Constructors, Hash Tables, & Linked Lists

    I am guessing you probably meant to say 'class' rather than constructor. That aside, to retrieve your object from the Map and call its function use the get method defined in HashTable

    ((WordDetails)WordList.get("One")).methodToCallHere();

    A few notes about this code a) you have to make the cast because there are no generics when you define the HashTable and b) you should check for a null pointer in case the call to WordList.get returns null.

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

    illusion887 (December 1st, 2009)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Constructors, Hash Tables, & Linked Lists

    I would also make the map type safe if I was you so you wont need to do casts.

    final Map<String, WordDetails> wordList = new HashMap<String, WordDetails>();
     
    wordList.get("One").methodToCallHere(); // You can do this without the cast since we know that get("One") will return an object of type WordDetails.

    // Json

Similar Threads

  1. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 1st, 2009, 05:13 AM
  2. merging two tables from two diffrent htmls files using java
    By sukant_at in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 7th, 2009, 12:48 PM
  3. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM
  4. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM