English 中文(简体)
我要如何声明 2D 字符串阵列列表?
原标题:How do I declare a 2D String arraylist?

我想做类似这样的事情 ArrayList & lt; String< String< String> & gt; my list

我怎样才能创造它?

如何加入外部和内部名单?

如何将内部列表转换为简单的数组列表?

最佳回答

你可以一起走

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()));
问题回答

第一个阵列列表不是字符串的阵列列表,而是矩阵列表的阵列列表。

ArrayList<ArrayList<String>>
List<List<String>> array = new ArrayList<List<String>>();
...
array.add(new ArrayList<String>())
array.get(0).add("qqq");

数组.get(0) - 是一个内部列表

List<List<String>> super2dArray = new ArrayList<ArrayList<String>>()

这是持有字符串的矩阵列表的矩阵列表 。

还有ArrayList< String< String< String>> my list 没有任何意义,因为字符串不是一个收藏/列表,但我认为你理解这一点。未来读者可能不会这样做。

“https://stackoverflow.com/a/8896608/645270><这个答案 ,以了解为什么我选择左侧有List

实现你的愿望有两种方式。我为两者提供代码片断:

1. List<List< String> & gt; lol = 新的ArrayList< List< String> & gt; (;)

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[] 设置 =新的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!");
}

尝试一下这个 :

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] +"	");
            }
            System.out.println("
");
        }  
        System.out.println("
");      
    }
}

创建 2D 阵列, 如下面 -

原始数据类型的 < 强 > -

int[][] arr = new int[10][10];

用于自定义类的 < em% 强 > -

public class MyClass {
    int x;
    MyClass() { 
        this.x = 0
    }
}

MyClass[][] arr = new MyClass[10][10];
for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
        arr[i][j] = new MyClass();
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...