25

I want to do something like this ArrayList<String<String>> mylist

How can I create it?

How can I add to the external and internal list

and how can I convert the internal list to a simple array list?

amdixon
  • 3,753
  • 8
  • 24
  • 33
124697
  • 21,137
  • 63
  • 182
  • 307

6 Answers6

25

You can go with

List<List<String>> ls2d = new ArrayList<List<String>>();
List<String> x = new ArrayList<String>();
x.add("Hello");
x.add("world!");
ls2d.add(x);

System.out.println(Arrays.deepToString(ls2d.toArray()));
rmv
  • 3,145
  • 4
  • 24
  • 28
Luiggi Mendoza
  • 83,472
  • 15
  • 149
  • 315
12

The first array list isn't an array list of String, it's an ArrayList of ArrayList.

ArrayList<ArrayList<String>>
jmort253
  • 33,123
  • 11
  • 95
  • 117
Yevgeny Simkin
  • 27,096
  • 37
  • 131
  • 232
  • 4
    Crap, should've posted an answer instead of a comment :p – keyser May 26 '12 at 16:58
  • How do I add to the second list? – 124697 May 26 '12 at 16:59
  • user521190, the first array list isn't an array list of String, it's an arraylist of arraylist. – Yevgeny Simkin May 26 '12 at 16:59
  • I still don't understand why people declare `ArrayList` instead of `List`, remember to develop oriented to interfaces. Also, check [TypeList vs type ArrayList in Java](http://stackoverflow.com/q/2279030/1065197) – Luiggi Mendoza May 26 '12 at 17:06
  • That's a separate question. Here... let me google it for you... http://bit.ly/LEhHht – Yevgeny Simkin May 26 '12 at 17:08
  • 2
    I mean if you want to teach programming in Java, you should do it in the right way, not just the thing that can run :). – Luiggi Mendoza May 26 '12 at 17:09
  • you asked a question, and I answered it! :) – Yevgeny Simkin May 26 '12 at 17:10
  • @Dr.Dredel - Luiggi is right, it is helpful to explain *why* something works in StackOverflow answers. I'll add your comment to your answer in case one of the mods deletes them :) Please see [point #10 on this meta post](http://meta.stackexchange.com/a/118694/155826) for more info. – jmort253 May 27 '12 at 00:31
3
List<List<String>> array = new ArrayList<List<String>>();
...
array.add(new ArrayList<String>())
array.get(0).add("qqq");

array.get(0) - is a internal list

dbf
  • 6,251
  • 2
  • 30
  • 60
3
List<List<String>> super2dArray = new ArrayList<ArrayList<String>>()

This is an arraylist of arraylists holding strings.

Also ArrayList<String<String>> mylist makes no sense, because String is not a collection/list, but I think you understood that. Future readers might not though.

See this answer to see why I chose to have List on the left side.

Community
  • 1
  • 1
keyser
  • 18,349
  • 16
  • 58
  • 97
2

There are two ways to achieve what you desire. I am providing code snippet for both:

1.List<List<String>> lol = new ArrayList<List<String>>();

Scanner in = new Scanner(System.in);
int size = in.nextInt();

//Declare your two dimensional ArrayList of Strings. 
List< List<String>> lol = new ArrayList<List<String>>();

//Instantiate and Populate
for (int i=0;i<size;i++){
    int internalListSize = in.nextInt(); //the size of each internal list
    lol.add(new ArrayList<String>());
    for (int j=0;j<internalListSize;j++){
        String whateverYouWanttoPut = in.nextLine();
        lol.get(i).add(whateverYouWanttoPut);
    }
}

//Access Elements 
try {
    System.out.println(lol.get(0).get(4));
    System.out.println(lol.get(1).get(2));
    System.out.println(lol.get(3).get(2));
} catch (Exception e){
    System.out.println("ERROR!");
}     

2. ArrayList[] set = new ArrayList[n];

Scanner in = new Scanner(System.in);
int size = in.nextInt();
//Declare your two dimensional ArrayList of Strings.
ArrayList[] set = new ArrayList[size];

//Populate it. 
for(int i=0;i<size;i++){
    int innerSize = in.nextInt();
    set[i] = new ArrayList<String>();
    for(int j=0;j<innerSize;j++){  
        set[i].add(in.nextLine());                
    }
}        
try{
    System.out.println(set[0].get(1));
    System.out.println(set[1].get(2));
} catch(Exception e){
    System.out.println("ERROR!");
}
Shwetabh Shekhar
  • 1,980
  • 21
  • 31
1

Try this :

public class JavaTests {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

    String[ ][ ] test2 = new String[3][3];      //3 can be replace if you add more test2[?][?] = "RandomString"

        test2[0][0] = "String1";
        test2[0][1] = "String2";
        test2[0][2] = "String3";

        test2[1][0] = "String4";
        test2[1][1] = "String5";
        test2[1][2] = "String6";

        test2[2][0] = "String7";
        test2[2][1] = "String8";
        test2[2][2] = "String9";

        for (int i = 0; i <= 2; i++){
            for (int j = 0; j <= 2; j++){
                System.out.print(test2[i][j] +"\t");
            }
            System.out.println("\n");
        }  
        System.out.println("\n");      
    }
}
SnareChops
  • 12,721
  • 9
  • 68
  • 90
LOL
  • 13
  • 4