如何在java中将字符串数组拆分为小块数组? [英] How to split a string array into small chunk arrays in java?

查看:1601
本文介绍了如何在java中将字符串数组拆分为小块数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是需要帮助的代码段示例

Below is the example of the code snippet which needs the help

示例:

[1,2,3,4,5]




  • 如果块大小 1 [1,2,3,4,5]

  • 如果块大小 2 [1,2] [3,4] [5]

  • 如果块大小 3 [1,2,3] [4,5]

  • 如果块大小 4 [1,2,3,4] [5]

    • if the chunk size is 1, [1,2,3,4,5]
    • if the chunk size is 2, [1,2] and [3,4] and [5]
    • if the chunk size is 3, [1,2,3] and [4,5]
    • if the chunk size is 4, [1,2,3,4] and [5]
    • Java(来自评论) :

      Java (from comment):

      int counter = 0;
      for (int i=0; i<array.length; i++) {
        if (count == chunksize) {
          //do something and initialize
          counter = 0;
        }
        counter++; 
      }
      


      推荐答案

      您可以使用 Arrays.copyOfRange(int [] original,int from,int to)
      代码可以是这样的:

      You can use Arrays.copyOfRange(int[] original, int from, int to) The code could be something like this:

      int chunk = 2; // chunk size to divide
      for(int i=0;i<original.length;i+=chunk){
          System.out.println(Arrays.toString(Arrays.copyOfRange(original, i, Math.min(original.length,i+chunk))));
      }          
      

      这篇关于如何在java中将字符串数组拆分为小块数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆