将数组截取

本文介绍了一种Java方法,用于将大型List分割成较小的子List,并将List中的元素转换为特定格式的字符串。这种方法适用于需要批量处理大量数据的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package text.work;

import java.util.ArrayList;
import java.util.List;


public class Text {
 private static List idList = new ArrayList();
 private static List resultList = new ArrayList();
    /**
     * 将List中的数据组成用逗号分隔的字符串,如'a','b','c' 
     * @param strList
     * @return
     */
 public static String getStr(List<String> strList) {
        String resultStr = "";
         if (strList != null && strList.size() > 0) {
          for (int i = 0; i < strList.size(); i++) {
           resultStr = resultStr + "'" + strList.get(i) + "'" + ',' ;
              }
              resultStr = resultStr.substring(0, resultStr.length() - 1);
        }
         return resultStr;
 }
 /**
  * 传入数组大于设定条数,则按设定的条数进行分解
  * eg:数组:'a','b','c','d','e','f','g'
  * 传入设定值为2
  * 则返回:    resultList.get(0) = 'a','b'
  *    resultList.get(1) = 'c','d'
  *    resultList.get(2) = 'e','f'
  *    resultList.get(3) = 'g'
  * @param list
  * @param updateCount
  * @return resultList
  */
 @SuppressWarnings({ "rawtypes", "unchecked" })
 public static List resultList(List<String> list,int updateCount){
  List l = new ArrayList();
  if(list.size() > updateCount){
   for(int i = 0;i < updateCount;i ++){
    l.add(list.get(0));
    list.remove(0);
   }
   resultList.add(getStr(l));
   if(list.size() > updateCount){
    resultList(list,updateCount);
   }else{
    resultList.add(getStr(list));
   }
  }else{
   resultList.add(getStr(list));
  }
  return resultList;
 }
 /**
  * @param args
  */
 @SuppressWarnings("unchecked")
 public static void main(String[] args) {
  Text();
  List resultList = resultList(idList,10);
  System.out.println(resultList);
  System.out.println(resultList.size());
  for(int i = 0;i < resultList.size();i++){
   System.out.println(resultList.get(i));
  }
 }
 @SuppressWarnings("unchecked")
 private static void Text() {
  for(int i = 0;i < 76;i++){
   idList.add(i+"");
  }
 }
}

测试结果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值