[Java] 将大数组拆分成小数组 →→→→→进入此内容的聊天室

来自 , 2019-06-29, 写在 Java, 查看 101 次.
URL http://www.code666.cn/view/428e20bb
  1. //定义一个长度为X的数组
  2.                 String[] all = new String[10];
  3.                 for (int i = 0; i < 10; i++) {
  4.                         all[i]="组"+i;
  5.                 }
  6.                 //定义每个小数组的长度
  7.                 int SMS_GROUP_NUMER = 3;
  8.                
  9.  
  10.                 int length = all.length;
  11.                 int num = length / SMS_GROUP_NUMER;
  12.                 num = length % SMS_GROUP_NUMER != 0 ? num + 1 : num;
  13.                 System.out.println(num + " 取整" + (length % SMS_GROUP_NUMER == 0));
  14.                
  15.                 //按照最大长度的小数组分组
  16.                 for (int j = 0; j < num - 1; j++) {
  17.                         String[] s = new String[SMS_GROUP_NUMER];
  18.                         for (int i = 0; i < SMS_GROUP_NUMER; i++) {
  19.                                 s[i] = all[i + j * SMS_GROUP_NUMER];
  20.                                 System.out.print("大数组:all["+(i + j * SMS_GROUP_NUMER) + "] -> 小组:s[" + i
  21.                                                 + "]" + ": [" + s[i] + "];  ");
  22.                         }
  23.                         System.out.println("##");
  24.                 }
  25.  
  26.                
  27.                 int roundFigure = length % SMS_GROUP_NUMER!=0?length % SMS_GROUP_NUMER:SMS_GROUP_NUMER;
  28.                 System.out.println(roundFigure+ " ### " + length);
  29.                
  30.                 //按照多出来的数据进行分组
  31.                 String[] extra = new String[roundFigure];
  32.                 for(int j= 0;j<roundFigure;j++){
  33.                         extra[j]= all[j+(num-1)*SMS_GROUP_NUMER];
  34.                         System.out.println("大数组:all[" + (j+(num-1)*SMS_GROUP_NUMER)+ "] -> 小组: extra_s["+j+"] : ["+extra[j]+"]");
  35.                 }
  36.                 System.out.println(SMS_GROUP_NUMER+(SMS_GROUP_NUMER)*(length % SMS_GROUP_NUMER)+"---------------");
  37.                
  38. //java/1311

回复 "将大数组拆分成小数组"

这儿你可以回复上面这条便签

captcha