用java中的两个1d数组填充2d数组
我有2个1d数组,并试图将它们填充到JAVA中的单个2d数组中。用java中的两个1d数组填充2d数组
例如:
x[] = {2,5,7,9}
y[] = {11,22,33,44}
结果应该然后就:
result[][] = {{2,5,7,9}, {11,22,33,44}}
我怎么去呢?目前,我有这样的事情:
for(int row = 0; row < 2; row++) {
for(int col = 0; col == y.length; col++) {
???
}
}
林之类的卡从那里......
二维数组是数组的数组。那么你为什么不尝试这个?
int result[][] = {x,y};
,并确保它是如此简单和工程,测试:
for(int i=0; i<result.length; i++)
{
for(int j=0; j<result[0].length; j++)
System.out.print(result[i][j]+ " ");
System.out.println();
}
你要恢复的行和列索引
for(int row = 0; row < 2; row++)
{
for(int col = 0; col = y.length; col++)
{
....
}
}
确定。第二个循环里面有什么? '结果[] []' – Buki 2012-02-17 07:27:58
试试这个:
ArrayList<Integer[]> tempList = new ArrayList<Integer[]>();
tempList.add(x);
tempList.add(y);
Integer result[][] = new Integer[tempList.size()][];
result = tempList.toArray(tempList);
谢谢,但我使用的不是数组列表。 :) – Buki 2012-02-17 07:37:35
arraylist只是一个数组的临时容器。您可以将多个不同大小的数组添加到数组列表中,然后使用toArray()从数组中获取相应的2D数组。 但是驱逐者的答案显然更简单,如果它适合您的需求。 – 2012-02-17 07:58:14
哈哈哈。这太尴尬了!谢谢。 x – Buki 2012-02-17 07:36:01
:)为什么人们会给出其他答案。 +1这个。 – 2012-02-17 07:38:33