我有以下结构 -
{
String[][] array = null;
array = getTable2C(Order order, v.get(i));
return array;
}
现在,我想添加一个新代码,我将获得多个2-D String数组&我想在方法结束时返回整个连接的2-D String数组。
String[][] array = null;
Vector v = getVectorFromSC(Order order);
for(int i=0;i<v.size();i++)
{
array = getTable2C(Order order, v.get(i));
/*
.
. Some code to Concatenate multiple 2D array.
. Something like, allArray = array +allArray;
.
*/
}
return allArray;
如何实现这一目标?我尝试将Array转换为Vector&amp;再次将Vector转换为数组(Vector的toArray()方法)但是,它适用于一维数组。我不知道如何将它用于2D数组。
答案 0 :(得分:0)
您可以尝试这样的事情:
1->Vector<String[][]> allArray = new Vector<String[][]>(); //DEfine a Vector that can hold 2D array
2-> allArray.add(array); //Inside for loop keep adding each 2D array to vector
3-> return allArray //Returns a vector which element is 2D array
答案 1 :(得分:0)
试试这个: -
public String[][] yourMethod(){
String[][] array = new String[0][0];
Vector v = getVectorFromSC(Order order);
for(int i=0;i<v.size();i++)
{
String[][] tempArray = getTable2C(Order order, v.get(i));
array = concat(array,tempArray);
}
return allArray;
}
public String[][] concat(String[][] a, String[][] b) {
String[][] result = new String[a.length + b.length][];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}