0

当我发现netbeans给我一个关于以下代码的错误时,我正在编码:

String [][]table={getRowArray()};
//getRowArray() will return me a 1D string array

有没有办法将我的方法返回的一维数组直接存储到二维数组中,而不是使用 for 循环?感谢您提供的任何帮助!

4

3 回答 3

2

对于初学者 - 是的,有可能做到这一点。

public class Test {
    public String[] dum() {
        String[] x = {"Not sure"};
        return x;
    }

    public static void main(String[] args) {
        Test t = new Test();
        String[][] words = {t.dum()};
        System.out.println(words[0][0]); // prints "Not sure"
    }
}

仔细检查您遇到的错误。确保返回类型getRowArray()为 true String[]

于 2012-05-22T04:24:33.373 回答
0

对此可能有更好的解决方案,但我相信您需要为其中一个维度指定单个索引才能存储它,因此您将一维数组存储到第一维的单个索引中。

IE

String [][]table;
table[0] = getRowArray();

这应该可以工作,因为数组的第一个维度存储其他数组

于 2012-05-22T04:17:29.710 回答
0
String [] oneD = { "1", "2", "3", "4" }; // getRowArray();
String [][] twoD1 = { oneD };
String [] anotherOneD = null; // or filled
String [][] twoD2 = { oneD, anotherOneD };
String [][] twoD3 = new String[][] { oneD };
String [][] twoD4 = new String[][] { oneD, anotherOneD };
于 2012-05-22T04:34:48.407 回答