我想将数组拆分为多个较小的数组。下面是我用于获取数组值的代码。
itemtbldata = itemtbldata.substring(1, itemtbldata.length()-1);
System.out.println("itemtbldata "+itemtbldata);
String[] itemcell = itemtbldata.split(",");
System.out.println(itemcell.length);
例如,itemcell的值低于
itemcell = [renu,1252,ed,dev,kalam,8562,ed,dev]
现在我想得到如下。请有人帮忙
arr1 = [renu,1252,ed,dev]
arr2 = [kalam,8562,ed,dev]
答案 0 :(得分:1)
您可以像这样使用System.arraycopy
(请参阅工作演示http://rextester.com/RZA81274)
int finalRowSize = 4; //Set here the chunks size
String[] itemcell = {"renu","1252","ed","dev","kalam","8562","ed","dev"};
String[][] result = monoToBidi(itemcell,itemcell.length / finalRowSize, finalRowSize);
public static String[][] monoToBidi(final String[] array, final int rows, final int cols){
String[][] bidi = new String[rows][cols];
for ( int i = 0; i < rows; i++ )
System.arraycopy(array, (i*cols), bidi[i], 0, cols);
return bidi;
}
答案 1 :(得分:0)
这是一个手动编写的 Java 方法:
public static void main(String[] args)
{
int max = 4;
String[] data = { "renu", "1252", "ed", "dev", "kalam", "8562", "ed", "dev", "hi", };
// use ceiling so 9 / 4 = 3. (round shouldn't be necessary, but just for safety, here it is)
int numOfArrays = (int)Math.round(Math.ceil(data.length / (float)max));
String[][] data2 = new String[numOfArrays][];
// loop over all instances except the last one (as that one might be smaller than the max
for (int i = 0; i < numOfArrays-1; i++)
{
data2[i] = new String[max];
System.arraycopy(data, i*max, data2[i], 0, max);
}
if (numOfArrays > 0)
{
int index = numOfArrays-1;
int length = data.length % max;
data2[index] = new String[length];
System.arraycopy(data, index*max, data2[index], 0, length);
}
System.out.println(Arrays.deepToString(data2));
}