如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
/**
* ArraySplit.java
* Copyright(C) 2014
*/
package com.udpdemo.test2;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author cuiran
* @version 1.0.0
*/
public class ArraySplit {
/**
* @param args
*
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int splitSize = 5 ; //分割的块大小
Object[] subAry = splitAry(ary, splitSize); //分割后的子块数组
for (Object obj: subAry){ //打印输出结果
int [] aryItem = ( int []) obj;
for ( int i = 0 ; i < aryItem.length; i++){
System.out.print(aryItem[i] + ", " );
}
System.out.println();
}
}
/**
* splitAry方法<br>
* @param ary 要分割的数组
* @param subSize 分割的块大小
* @return
*
*/
private static Object[] splitAry( int [] ary, int subSize) {
int count = ary.length % subSize == 0 ? ary.length / subSize: ary.length / subSize + 1 ;
List<List<Integer>> subAryList = new ArrayList<List<Integer>>();
for ( int i = 0 ; i < count; i++) {
int index = i * subSize;
List<Integer> list = new ArrayList<Integer>();
int j = 0 ;
while (j < subSize && index < ary.length) {
list.add(ary[index++]);
j++;
}
subAryList.add(list);
}
Object[] subAry = new Object[subAryList.size()];
for ( int i = 0 ; i < subAryList.size(); i++){
List<Integer> subList = subAryList.get(i);
int [] subAryItem = new int [subList.size()];
for ( int j = 0 ; j < subList.size(); j++){
subAryItem[j] = subList.get(j).intValue();
}
subAry[i] = subAryItem;
}
return subAry;
}
}
|
以上这篇将一个数组按照固定大小进行拆分成数组的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。