Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 26th, 2006, 10:44 PM   #1
ELHEK
Newbie
 
Join Date: May 2006
Posts: 9
Rep Power: 0 ELHEK is on a distinguished road
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!
ELHEK is offline   Reply With Quote
Old Sep 26th, 2006, 10:55 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
I believe you would create an array of lists just like you would create an Array of anything else.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Sep 26th, 2006, 11:41 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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;
// ...
titaniumdecoy is offline   Reply With Quote
Old Sep 27th, 2006, 4:19 PM   #4
metsfan
Programmer
 
Join Date: Dec 2005
Posts: 57
Rep Power: 0 metsfan is an unknown quantity at this point
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");
...
metsfan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning an array of lists deanosrs C 42 Apr 13th, 2006 1:35 PM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 8:19 PM
Changing Array structures to Linked Lists? kalulu C 4 Nov 29th, 2005 6:33 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM
Converting 1-dimensional array to 2-dimensional array Tazz_Mission_13 Java 6 Apr 8th, 2005 11:58 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC