package org.jimmy.autosearch2019.test;

import java.util.ArrayList;

public class Test20190327 {

    public static void main(String[] args) {
        ArrayList<String> strList = new ArrayList<String>();
        for(int i = 1; i < 22; i++) {
            strList.add(i + "");
        }
        int group = 2;
        ArrayList<String> groupedStrList = getGroupStrs(strList, group);
        for(int i = 0; i < groupedStrList.size(); i++) {
            System.out.println(groupedStrList.get(i));
        }
    }

    public static ArrayList<String> getGroupStrs(ArrayList<String> strList, int group){
        ArrayList<String> groupedStrList = new ArrayList<String>();
        String strs = "";
        for(int i = 0; i < strList.size(); i++) {
            if(i % group == group - 1) {
                strs += strList.get(i);
                groupedStrList.add(strs);
                strs = "";
            }else {
                strs += strList.get(i) + ",";
            }
            if(i == strList.size() - 1) {
                if(strs.lastIndexOf(",") > 0 && strs.lastIndexOf(",") == strs.length() - 1) {
                    strs = strs.substring(0, strs.length() - 1);
                }
                groupedStrList.add(strs);
            }
        }
        return groupedStrList;
    }
    
}

以上就是代码.

效果图:如下

Java以组的数量将字符串分组

 

相关文章:

  • 2022-01-24
  • 2021-11-18
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2021-11-01
  • 2021-05-17
  • 2022-12-23
  • 2021-10-29
相关资源
相似解决方案