Results 1 to 5 of 5
- 02-21-2011, 03:40 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
How to access an element of a linked list inside another linked list?
I am trying to read a few sentences, then segment them into smaller units (phrases). So I am using a Linked list for all the sentences, each element of this linked list is another linked list contains the phrases. I am done with adding the elements, but having trouble of accessing them.
here's my code
but how can I get the second element of second linked list? for exampleJava Code:LinkedList outerlist = new LinkedList(); tempSent= in.readLine(); // initialized BufferReader while(i < numSent){ StringTokenizer tok = new StringTokenizer(tempSent, " "); LinkedList templist = new LinkedList(); while (tok.hasMoreTokens()){ templist.add(tok.nextToken()); } outerlist.add(templist); temp = in.readLine(); i ++; } // this works fine it prints the whole list System.out.println(outerlist); // this works fine too it prints the second linked list System.out.println(outerlist.get(2));.Java Code:outerlist.get(2).get(2) // I know this does not work but that's what i need
any help would be truly appreciated!
- 02-21-2011, 03:55 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
maybe it is something wrong with casting
the get(int index) method's return type is Object,so maybe you must cast it to the type of LinkedList like this:
Java Code:((LinkedList)(outlist.get(2))).get(2)
- 02-21-2011, 03:58 AM #3
Or better yet use Generics.
Java Code:LinkedList<LinkedList<String>> outerlist = new LinkedList<LinkedList<String>>();
- 02-21-2011, 08:58 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Thank you guys they worked well!
(linked list is slow though thats what i heard...might use something else in the end)
- 02-21-2011, 09:34 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
There's nothing intrinsically slow about linked lists; they hop from node to node when you iterate over them Finding the i-th element can be slow for long lists. If they are too slow for you you might consider using ArrayLists instead with a large enough initial capacity. (read the API documentation for that class for details).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
access class members with linked list
By billq in forum New To JavaReplies: 5Last Post: 05-09-2010, 05:04 PM -
Convert Linked List Object element to String
By CirKuT in forum New To JavaReplies: 2Last Post: 12-13-2008, 05:22 AM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM -
Linked List help
By neobie in forum New To JavaReplies: 8Last Post: 12-22-2007, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks