0

我有一个来自 .split 命令的数组,想将它放入一个名为 String[][] datatabvars 的数组中,我不知道如何将 datatabvars 转换为二维数组并将数据放入其中。

public String[] getList() {
    String file_name = "path";
    String[] links = null;
    String[][] datatabvars = null; // this var
    int numberOfDatatabs = 0;
    try {
        ReadFile file = new ReadFile(file_name); 
        String[] aryLines = file.OpenFile();

            int i;
        for(i=0; i < aryLines.length; i++) { //aryLines.length
            if (aryLines[i].substring(0, 7).equals("datatab")) {
                aryLines[i] = aryLines[i].replace("datatab["+Integer.toString(numberOfDatatabs)+"] = new Array(", "");
                aryLines[i] = aryLines[i].replace(");", "");
                datatabvars = aryLines[i].split(","); // this split array
                numberOfDatatabs++;
            }
        }
        System.out.println(datatabvars[0]);
    }catch (IOException e) {
        System.out.println( e.getMessage() );
    }
    return links;
}
4

2 回答 2

3

如下更新两行(我添加了评论):(我假设您的其余代码正在运行)

String[][] datatabvars = null; // this var
int numberOfDatatabs = 0;
try {
    ReadFile file = new ReadFile(file_name); 
    String[] aryLines = file.OpenFile();
    datatabvars = new String[aryLines.length][]; // INITIALIZED

    int i;
    for(i=0; i < aryLines.length; i++) { //aryLines.length
        if (aryLines[i].substring(0, 7).equals("datatab")) {
            aryLines[i] = aryLines[i].
                        replace("datatab["+Integer.toString(numberOfDatatabs)+"] = 
                                               new Array(", "");
            aryLines[i] = aryLines[i].replace(");", "");
            datatabvars[i] = aryLines[i].split(","); // this split array: ASSIGNED
            numberOfDatatabs++;
        }
    }
    System.out.println(datatabvars[0]);
于 2012-12-07T23:26:06.683 回答
1

通常,要避免像瘟疫一样避免使用数组-如果可能的话,请使用集合:。在这种情况下,split()返回 a String[],所以使用它,但用于List<String[]>存储多个String[]

List<String[]> datatabvars = new ArrayList<String[]>();
...
String[] array = input.split(",");
datatabvars.add(array);

你会发现使用集合比使用数组更容易。

于 2012-12-07T23:43:32.307 回答