Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   array of listed lists (http://www.programmingforums.org/showthread.php?t=11409)

ELHEK Sep 26th, 2006 11:44 PM

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!

Mjordan2nd Sep 26th, 2006 11:55 PM

I believe you would create an array of lists just like you would create an Array of anything else.

titaniumdecoy Sep 27th, 2006 12:41 AM

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>();
dict.add(linkedListObject1);
dict.add(linkedListObject2);
// ...

If your question is how to store LinkedList objects in a simple array, remember that all objects in Java are stored as references. Therefore, you can create an array of LinkedList objects easily; you cannot, however, alter the number of elements the array can store once it has been created. (By default, all indices are set to null.) You can do this as follows:

:

LinkedList[] dict = new LinkedList[NUM_ELEMENTS];
dict[0] = linkedListObject1;
dict[1] = linkedListObject2;
// ...


metsfan Sep 27th, 2006 5:19 PM

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 array
a[0] = 0;  // intialize indexes
a[1] = 1;
...


As 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 array

Now, 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++)
{
dictionary[i] = new LinkedList();
}


Your dictionary is now ready to go!

Now we just use it as we would any linked list:

:

dictionary[0].add("apple");
dictionary[0].add("amazing");
dictionary[1].add("ball");
...



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