将一个数组按照固定大小进行拆分成数组的方法
程序员文章站
2024-03-11 11:54:49
如下所示:
/**
* arraysplit.java
* copyright(c) 2014
*/
package com.udpdemo.tes...
如下所示:
/** * 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[] ary = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};//要分割的数组 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; } }
以上这篇将一个数组按照固定大小进行拆分成数组的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。