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:
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]
}
//-------------------------------------------------------------------------------------------------------------------
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
Code :
((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.
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.
Code :
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