Results 1 to 5 of 5
  1. #1
    smtwtfs is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default 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
    Java 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));
    but how can I get the second element of second linked list? for example
    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!

  2. #2
    liyuyu_852000 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default 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)

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,802
    Rep Power
    9

    Default

    Or better yet use Generics.
    Java Code:
    LinkedList<LinkedList<String>> outerlist = new LinkedList<LinkedList<String>>();

  4. #4
    smtwtfs is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    Thank you guys they worked well!

    (linked list is slow though thats what i heard...might use something else in the end)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,189
    Blog Entries
    7
    Rep Power
    24

    Default

    Quote Originally Posted by smtwtfs View Post
    Thank you guys they worked well!

    (linked list is slow though thats what i heard...might use something else in the end)
    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,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  2. access class members with linked list
    By billq in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 05:04 PM
  3. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 06:22 AM
  4. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  5. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •