我有一个数组,例如:
{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }
我想把它分解成子数组。当我测试它时,它显示错误:
java.lang.ArrayStoreException at line: String[] strarray = splitted.toArray(new String[0]);
代码:
public static String[] splittedArray(String[] srcArray) {
List<String[]> splitted = new ArrayList<String[]>();
int lengthToSplit = 3;
int arrayLength = srcArray.length;
for (int i = 0; i < arrayLength; i = i + lengthToSplit) {
String[] destArray = new String[lengthToSplit];
if (arrayLength < i + lengthToSplit) {
lengthToSplit = arrayLength - i;
}
System.arraycopy(srcArray, i, destArray, 0, lengthToSplit);
splitted.add(destArray);
}
String[] strarray = splitted.toArray(new String[0]);
return strarray;
}