目录
说明:我这里写的代码的 限制条件是 一维数组中最多 有 26个 元素(以26个字母为基本)。当然,想测试更多,可以更改判断条件。
代码如下:
说明:输出二维数组的逻辑,参考JDK的源码。JDK有现成的API:Arrays.deepToString:
@SuppressWarnings("all")
public class ArrayTransform {
private static final int
ZERO = 0, ONE = 1, TWO = 2, THREE = 3, FOUR = 4,
FIVE = 5, SEX = 6, SEVEN = 7, EIGHT = 8, NINE = 9,
TEN = 10, ELEVEN = 11, TWELVE = 12, THIRTEEN = 13,
FOURTEEN = 14, FIFTEEN = 15, SIXTEEN = 16, SEVENTEEN = 17,
EIGHTEEN = 18, NINETEEN = 19, TWENTY = 20, TWENTY_ONE = 21,
TWENTY_TWO = 22, TWENTY_THREE = 23, TWENTY_FOUR = 24, TWENTY_FIVE = 25,
TWENTY_SIX = 26;
public static Object[][] transformToTwoDimensionArray(Object[] srcArray, int howGap) throws Exception {
if (srcArray.length > TWENTY_SIX) {
throw new Exception("sorry, at most 26 element!!!");
}
if (howGap == ZERO){
throw new Exception("sorry, how Gap cannot is zero !!!");
}
if (srcArray.length == ZERO){
return new Object[ZERO][];
}
Object[][] objDimensionArr = null;
int flag = ZERO;
// 分配二维数组长度
if ((howGap == srcArray.length)) {
flag = ONE;
objDimensionArr = new Object[ONE][];
} else {
objDimensionArr = allocationTwoDimensionArrayLength(srcArray, howGap, objDimensionArr);
}
int count = 0;
for (int j = 0; j < objDimensionArr.length; j++) {
// 为二维数组里面的一维数组分配长度
if (flag == ONE) {
objDimensionArr[j] = new Object[srcArray.length];
} else {
objDimensionArr[j] = new Object[howGap];
if (j == objDimensionArr.length - 1) {
switch (srcArray.length % howGap) {
case ONE:
objDimensionArr[j] = new Object[ONE];
break;
case TWO:
objDimensionArr[j] = new Object[TWO];
break;
case THREE:
objDimensionArr[j] = new Object[THREE];
break;
case FOUR:
objDimensionArr[j] = new Object[FOUR];
break;
case FIVE:
objDimensionArr[j] = new Object[FIVE];
break;
case SEX:
objDimensionArr[j] = new Object[SEX];
break;
case SEVEN:
objDimensionArr[j] = new Object[SEVEN];
break;
case EIGHT:
objDimensionArr[j] = new Object[EIGHT];
break;
case NINE:
objDimensionArr[j] = new Object[NINE];
break;
case TEN:
objDimensionArr[j] = new Object[TEN];
break;
case ELEVEN:
objDimensionArr[j] = new Object[ELEVEN];
break;
case TWELVE:
objDimensionArr[j] = new Object[TWELVE];
break;
}
}
}
// 给 每个二维数组里面的一位数组的元素 赋值
to:
for (int i = 0; i < srcArray.length; i++) {
for (int d = 0; d < objDimensionArr[j].length; d++) {
objDimensionArr[j][d] = srcArray[count];
count++;
if (d == objDimensionArr[j].length - 1) {
break to;
}
}
}
}
return objDimensionArr;
}
private static Object[][] allocationTwoDimensionArrayLength(Object[] srcArray, int howGap, Object[][] objDimensionArr) throws Exception {
// if (srcArray.length / howGap == srcArray.length) {
// objDimensionArr = new Object[srcArray.length][];
// }
if (srcArray.length / howGap == TWENTY_SIX) {
objDimensionArr = new Object[TWENTY_SIX][];
} else {
if (srcArray.length % howGap == 0) {
objDimensionArr = schema1(srcArray, howGap);
} else {
if (howGap > (srcArray.length / 2)) {
switch (srcArray.length / howGap) {
case ONE:
objDimensionArr = new Object[ONE + 1][];
break;
default:
objDimensionArr = schema2(srcArray, howGap);
}
} else {
objDimensionArr = schema2(srcArray, howGap);
}
}
}
return objDimensionArr;
}
private static Object[][] schema2(Object[] srcArray, int howGap) throws Exception {
Object[][] objDimensionArr = null;
switch (srcArray.length / howGap) {
case ONE:
objDimensionArr = new Object[ONE + 1][];
break;
case TWO:
objDimensionArr = new Object[TWO + 1][];
break;
case THREE:
objDimensionArr = new Object[THREE + 1][];
break;
case FOUR:
objDimensionArr = new Object[FOUR + 1][];
break;
case FIVE:
objDimensionArr = new Object[FIVE + 1][];
break;
case SEX:
objDimensionArr = new Object[SEX + 1][];
break;
case SEVEN:
objDimensionArr = new Object[SEVEN + 1][];
break;
case EIGHT:
objDimensionArr = new Object[EIGHT + 1][];
break;
case NINE:
objDimensionArr = new Object[NINE + 1][];
break;
case TEN:
objDimensionArr = new Object[TEN + 1][];
break;
case ELEVEN:
objDimensionArr = new Object[ELEVEN + 1][];
break;
case TWELVE:
objDimensionArr = new Object[TWELVE + 1][];
break;
default:
if (howGap > srcArray.length) {
throw new Exception("sorry, gap cannot greater than resource array length!!! array length: " + srcArray.length);
}
}
return objDimensionArr;
}
private static Object[][] schema1(Object[] srcArray, int howGap) {
Object[][] objDimensionArr = null;
switch (srcArray.length / howGap) {
case ONE:
objDimensionArr = new Object[ONE][];
break;
case TWO:
objDimensionArr = new Object[TWO][];
break;
case THREE:
objDimensionArr = new Object[THREE][];
break;
case FOUR:
objDimensionArr = new Object[FOUR][];
break;
case FIVE:
objDimensionArr = new Object[FIVE][];
break;
case SEX:
objDimensionArr = new Object[SEX][];
break;
case SEVEN:
objDimensionArr = new Object[SEVEN][];
break;
case EIGHT:
objDimensionArr = new Object[EIGHT][];
break;
case NINE:
objDimensionArr = new Object[NINE][];
break;
case TEN:
objDimensionArr = new Object[TEN][];
break;
case ELEVEN:
objDimensionArr = new Object[ELEVEN][];
break;
case TWELVE:
objDimensionArr = new Object[TWELVE][];
break;
case THIRTEEN:
objDimensionArr = new Object[THIRTEEN][];
break;
}
return objDimensionArr;
}
public static String dimensionArrayToString(Object[] oArr) {
if (oArr == null){return "null";}
int bufLength = oArr.length * 20;
if (oArr.length != 0 && bufLength <= 0){
bufLength = Integer.MAX_VALUE;
}
StringBuilder sb = new StringBuilder(bufLength);
toStr(oArr,sb);
return sb.toString();
}
private static void toStr(Object[] oArr, StringBuilder sb){
if (oArr == null){
sb.append("null");
return;
}
int maxIndex = oArr.length -1;
if (maxIndex == -1){
sb.append("[]");
return;
}
sb.append('[');
int i = 0;
while (true){
// 每个元素
Object ele = oArr[i];
if (ele == null ){
sb.append("null");
}else{
final Class<?> clazz = ele.getClass();
// 是否是数组,是就递归
if (clazz.isArray()){
toStr((Object[]) ele,sb);
}else{
// 数组的每个元素to str
sb.append(ele.toString());
}
}
if (i == maxIndex){
break;
}
sb.append(", ");
i++;
}
sb.append(']');
// for (int i=0;;i++){
// // 每个元素
// Object ele = oArr[i];
// if (ele == null ){
// sb.append("null");
// }else{
// final Class<?> clazz = ele.getClass();
// // 是否是数组,是就递归
// if (clazz.isArray()){
// // 递归
// to((Object[]) ele,sb);
// }else{
// //
// sb.append(ele.toString());
// }
// }
// if (i == maxIndex){
// break;
// }
// sb.append(", ");
// }
}
}
test:
public static void main(String[] args) throws Exception {
Character[] srcArray = {'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
final Object[][] resultArr = ArrayTransform.transformToTwoDimensionArray(srcArray, 3);
final String s = ArrayTransform.dimensionArrayToString(resultArr);
System.out.println(s);
}
result: