Skip to main content

APIzation: Replication Package

[Q#10768170][A#10768198] How do I declare a 2D String arraylist?

https://stackoverflow.com/q/10768170

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?

Answer

https://stackoverflow.com/a/10768198

You can go with

APIzation

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()));
package com.stackoverflow.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Human10768198 {

  public static List<List<String>> generate2DArrays() {
    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()));
    return ls2d;
  }
}

package com.stackoverflow.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * How do I declare a 2D String arraylist?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/10768198">https://stackoverflow.com/a/10768198</a>
 */
public class APIzator10768198 {

  public static String declareArraylist(List<String> x)
    throws Exception {
    List<List<String>> ls2d = new ArrayList<List<String>>();
    ls2d.add(x);
    return Arrays.deepToString(ls2d.toArray());
  }
}