java 如何声明2D字符串数组列表?

qmelpv7a  于 2023-09-29  发布在  Java
关注(0)|答案(8)|浏览(119)

我想做这样的事情ArrayList<String<String>> mylist
我该如何创建它?
我如何才能添加到外部和内部列表
如何将内部列表转换为简单的数组列表?

dl5txlt9

dl5txlt91#

你可以去与

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()));
h7wcgrx3

h7wcgrx32#

第一个数组列表不是String的数组列表,而是ArrayList的ArrayList。

ArrayList<ArrayList<String>>
pkbketx9

pkbketx93#

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

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

os8fio9y

os8fio9y4#

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

这是一个保存字符串的数组列表的数组列表。
ArrayList<String<String>> mylist也没有意义,因为String不是一个集合/列表,但我想你明白这一点。未来的读者可能不会。
请参阅this answer,了解为什么我选择在左侧使用List

tktrz96b

tktrz96b5#

试试这个:

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");      
    }
}
afdcj2ne

afdcj2ne6#

有两种方法可以实现你的愿望。我为这两个提供了代码片段:

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!");
}
6yt4nkrj

6yt4nkrj7#

创建2D数组,如下所示-

对于原始数据类型-

int[][] arr = new int[10][10];
  • 自定义类-*
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();
    }
}
rseugnpd

rseugnpd8#

我已经将answer code provided by @Luiggi Mendoza重新构造为一个Java方法,您可以轻松地将其合并到您的开发中:

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

public class Post10768198 {
    public static List<List<String>> create2DStringArrayList() {
        List<List<String>> ls2d = new ArrayList<List<String>>();
        List<String> x = new ArrayList<String>();
        x.add("Hello");
        x.add("world!");
        ls2d.add(x);
        return ls2d;
    }
}

相关问题