![]() |
array of listed lists
Hey everyone, im doing an project for uni, and i have to make a dictionary, which is an array of linked list, where each element of the array is a linked list which stores all the words starting with a certain letter of the alphabet.
the words for the dictionary are ment to be read in from a dictionary and then sorted in the array of linked lists. im still new at programming and cant understand how to make an array with each element consisting of a linked list. any help would be great! thx! |
I believe you would create an array of lists just like you would create an Array of anything else.
|
I'm not sure what your problem is. However, I would recommend using an ArrayList to store the LinkedList objects because it is much more flexible (you can add and remove elements easily). If you choose to go about your task in this way, you might use code similar to the following:
:
ArrayList<LinkedList> dict = new ArrayList<LinkedList>();:
LinkedList[] dict = new LinkedList[NUM_ELEMENTS]; |
Creating an array of objects is no different than creating an array of primary data types. Im assuming you know how to make an array of primary data types, but just to refresh your memory:
:
int[] a = new int[7]; //create the arrayAs you probably already know, this creates an array of 7 integers. Creating an array of LinkedList objects is no different. :
LinkedList[] dictionary = new LinkedList[26] //create the arrayNow, you must remember all that does is create the pointers in memory. None of these indexes are initialzed. In order to actually use the linked lists, you will have to initialize each one. a for loop does the trick: :
for(int i = 0; i < dictionary.length; i++) Your dictionary is now ready to go! Now we just use it as we would any linked list: :
dictionary[0].add("apple"); |
| All times are GMT -5. The time now is 12:56 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC