public class Test {
public static void main(String[] args) {
// SystemCode systemCode = SystemCode.OK;
// System.out.println(systemCode.getCode()+systemCode.getMessage());
// String encodeStr= DigestUtils.md5Hex("123456");
// System.out.println(encodeStr);
String arr[] = new String[]{"a", "b", "c", "d", "e", "f","g"};
splitArr( arr, 2 );
输出结果展示:
a,b
c,d
e,f
g
}
//arraycopy 参数分别是:源数组,源数组开始截取的位置,目标数组,目标数组开始生成数据的位置,目标数组的长度
public static void splitArr(String arr[], int splitSize) {
int a = arr.length / splitSize;
if (a == 0) {
System.out.println( transform( arr ) );
} else {
for (int i = 0; i <= a; i++) {
if (i < a) {
String narr[] = new String[splitSize];
System.arraycopy( arr, i * splitSize, narr, 0, splitSize );
System.out.println( transform( narr ) );
} else {
if (arr.length!=i*a) {
String narr[] = new String[arr.length - a * splitSize];
System.arraycopy( arr, a * splitSize, narr, 0, arr.length - a * splitSize );
System.out.println( transform( narr ) );
}
}
}
}
}
public static String transform(String[] arr) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
if (i < arr.length - 1) {
stringBuffer.append( arr[i] + "," );
} else {
stringBuffer.append( arr[i] );
}
}
return stringBuffer.toString();
}
}