资源描述:
《Java分割数组成子数组.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Java分割数组成子数组分类: Java2011-10-2012:51 5448人阅读 评论(0) 收藏 举报javaobjectlistimportstringclass 我有一个2000多长度的数组,我要分割成40个50长度的子数组,怎么实现?问题补充:我需要的结果是这样的:把这个数组int[]sum={0,1,2,3,4,5,……,18};分成int[]a={0,1,2,3,4};//长度5int[]b={5,6,7,8,9};//长度5int[]c={10,11,12,13,14};//长度5int[]d={15,16,17,18};//长度4[javascript] view
2、plaincopy1.import java.util.ArrayList; 2.import java.util.List; 3. 4.public class ABC { 5. 6. public static void main(String[] args) { 7. 8. int[] ary = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};//要分割的数组 9. int splitSize = 5;//分割的块大小 10. 11. Object[] subAry = splitAry(ar
3、y, splitSize);//分割后的子块数组 12. 13. for(Object obj: subAry){//打印输出结果 14. 15. int[] aryItem = (int[]) obj; 16. 17. for(int i = 0; i < aryItem.length; i++){ 18. System.out.print(aryItem[i] + ", "); 19. } 20. System.out.println(); 21. } 22. 23. 1. } 2. 3. private s
4、tatic Object[] splitAry(int[] ary, int subSize) { 4. int count = ary.length % subSize == 0 ? ary.length / subSize: ary.length / subSize + 1; 5. 6. List> subAryList = new ArrayList>(); 7. 8. for (int i = 0; i < count; i++) { 9. int index = i * subSize; 10.
5、 11. List list = new ArrayList(); 12. int j = 0; 13. while (j < subSize && index < ary.length) { 14. list.add(ary[index++]); 15. j++; 16. } 17. 18. subAryList.add(list); 19. } 20. 21. Object[] subAry = new Object[subAryList.size()]; 22. 23.
6、 for(int i = 0; i < subAryList.size(); i++){ 24. List subList = subAryList.get(i); 25. 26. int[] subAryItem = new int[subList.size()]; 27. for(int j = 0; j < subList.size(); j++){ 28. subAryItem[j] = subList.get(j).intValue(); 29. } 30. 31. subAry[i] = subAryI
7、tem; 32. } 33. 34. return subAry; 35. } 36.} ------------------0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,