Results 1 to 6 of 6
  1. #1
    alvations is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default How to access ArrayList in List of List?

    i've created a list that holds a list of arraylist. i'm trying to access a string in my arraylist but i can't get the syntax to access a specific string. anyone could give a hand?
    _________________________________________________
    List<List> sentences = new ArrayList<List>();

    List<List> each_sentence = new ArrayList<List>();

    ArrayList<String> iob = new ArrayList<String> ();
    ArrayList<String> pos = new ArrayList<String> ();
    ArrayList<String> word = new ArrayList<String> ();

    iob.add("pig");
    iob.add("cow");
    iob.add("horse");

    pos.add("eat");
    pos.add("ate");
    pos.add("eats");

    word.add("meat");
    word.add("grass");
    word.add("wheat");

    each_sentence.add(iob);
    each_sentence.add(pos);
    each_sentence.add(word);

    sentences.add(each_sentence);

    System.out.println(sentences.get(0).get(0));
    //this will print out [pig, cow horse]

    System.out.println(sentences.get(0).get(0).get(0)) ;
    //this will get a syntax error

    //is there a way to print just [pig]??

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    List<List<String>> bogus = new ArrayList<List<String>>();
    bogus.add(new ArrayList<String>());
    bogus.get(0).add("Hello");
    String result = bogus.get(0).get(0);
    bogus.add(new ArrayList<String>());
    bogus.get(1).add("Goodbye");
    for (List<String> l : bogus) {
      System.out.println("Start list");
      for (String s : l) {
        System.out.println("Entry:  " + s);
      }
    }
    Edit: IOW you must include the generic type all the way down, for a "triple" list it would be
    Java Code:
    List<List<List<String>>>
    Edit again: To make yours work as it is you need (I don't suggest it this way though)
    Java Code:
    ((List) sentences.get(0).get(0)).get(0)
    Last edited by masijade; 10-08-2008 at 11:29 AM.

  3. #3
    alvations is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    yep.

    String result = bogus.get(0).get(0); //this works

    but i need

    String result =bogus.get(0).get(0).get(0);

    your "hello" is added to bogus.get(0).add("hello");

    my string is stored in bogus.get(0).get(0).add("hello");

    i can't seem to access the 3rd level of the list.

    could you show me using my code? i'm confused when you use for(String s : l) and for(List<String> l : bogus) what does that mean?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I edited my post, read the two edits first, then post again if you still do not understand.

  5. #5
    alvations is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Thanks a million. the list type casting works wonders.

    i'll take note of how i build my list of list the next time. cos it's a long program and i've already managed to run it. so i'll leave the proper construct of List<List<List<String>>>.

    just one last question, can i conclude that the way i construct my list type will affect the level of data i can access?

    so if i only have List<List> i can only get(x).get(x) but if i have List<List<List<List<List<String>>>> i can get(x).get(x).get(x).get(X) ???

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes.

    In your example the "outer" list was declared simple List<List> so, when you do get(0) it has a List (not a List<List>) so when you do get(0).get(0), you have an Object (not a List<String>).

    If it where declared List<List<List>> the get (0) will return a List<List>> and get(0).get(0) will return a List (not a List<String>) and get(0).get(0).get(0) will return an Object (not a String).

    Declared List<List<List<String>>> the get (0) will return a List<List<String>> and get(0).get(0) will return a List<String> and get(0).get(0).get(0) will return a String.

Similar Threads

  1. List and ArrayList
    By ravian in forum New To Java
    Replies: 3
    Last Post: 07-27-2011, 05:05 PM
  2. deep copying arraylist to add to a list
    By alvations in forum New To Java
    Replies: 13
    Last Post: 10-08-2008, 03:13 PM
  3. How to use List in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:02 PM
  4. list
    By Zensai in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 07:22 AM
  5. How to get the max value from a list
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 05:43 PM

Posting Permissions

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