Java 平均分割数组list

14 篇文章 1 订阅
public static <T> ArrayList<ArrayList<T>> splitList(ArrayList<T> source, int n) {

   if (null == source || source.size() == 0 || n <= 0)
      return null;

   ArrayList<ArrayList<T>> result = new ArrayList<>();

   int sourceSize = source.size();
   int size;
   if (source.size() % n == 0) {
      size = source.size() / n;
   } else {
      size = (source.size() / n) + 1;
   }
   for (int i = 0; i < size; i++) {
      ArrayList<T> subset = new ArrayList<>();
      for (int j = i * n; j < (i + 1) * n; j++) {
         if (j < sourceSize) {
            subset.add(source.get(j));
         }
      }
      result.add(subset);
   }
   return result;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以尝试这样的代码:public static List<String> splitStringByByteSize(String s, int byteSize) { List<String> result = new ArrayList<>(); int len = s.length(); int start = 0; while (start < len) { int end = start + byteSize; if (end > len) { end = len; } String subStr = s.substring(start, end); result.add(subStr); start = end; } return result; } ### 回答2: 可以使用Java编写一个按照30个字节切分字符串的函数,具体代码如下: ```java public static List<String> splitByBytes(String str) { List<String> result = new ArrayList<>(); if (str == null || str.isEmpty()) { return result; } byte[] bytes = str.getBytes(); int length = bytes.length; int index = 0; int count = 0; StringBuilder sb = new StringBuilder(); while (index < length) { if (count + bytes[index] <= 30) { sb.append(str.charAt(index)); count += bytes[index]; index++; } else { result.add(sb.toString()); sb.setLength(0); count = 0; } } if (sb.length() > 0) { result.add(sb.toString()); } return result; } ``` 这个函数接收一个字符串作为输入,将字符串按照每个字节的长度进行切分,每个切分后的子字符串都不超过30个字节。它使用了byte数组来获取每个字符的字节长度,并根据字节长度进行切分。在遍历字符串过程中,使用一个计数器count来记录已经累计的字节数,当累计的字节数加上下一个字符的字节数大于30时,将已构建的子字符串添加到结果列表中,然后重置计数器和StringBuilder对象,继续切分下一个子字符串。最后,如果有未达到30字节的子字符串,将其添加到结果列表中。函数返回一个List,其中包含切分后的所有子串。 需要注意的是,由于Java的String类使用的是UTF-16编码,每个字符所占的字节数不同,因此直接按照字符长度进行切分可能会出现切分位置不准确的情况。为了确保准确切分,我们使用了字节数来进行判断和计数。 ### 回答3: 你好!下面是使用Java编写的按30个字节切分字符串的函数的示例代码: ```java public class StringUtils { public static List<String> splitStringByByteSize(String input) { List<String> result = new ArrayList<>(); int maxLength = 30; // 最大字节长度为30 if (input == null || input.length() == 0) { return result; } StringBuilder builder = new StringBuilder(); int byteCount = 0; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); int charByteSize = calculateByteSize(c); // 当前字符的字节数与当前字节数之和小于等于30,则加入builder中 if (byteCount + charByteSize <= maxLength) { builder.append(c); byteCount += charByteSize; } else { // 超过30个字节,则将builder添加到结果中,重新开始计数 result.add(builder.toString()); builder.setLength(0); builder.append(c); byteCount = charByteSize; } } // 添加最后一个builder的内容到结果中 result.add(builder.toString()); return result; } // 计算字符的字节数 private static int calculateByteSize(char c) { return String.valueOf(c).getBytes().length; } public static void main(String[] args) { String input = "这是一个由超过30个字节的字符串example"; List<String> result = splitStringByByteSize(input); for (String str : result) { System.out.println(str); } } } ``` 该函数将输入的字符串按照30个字节切分,并将切分后的结果存储在一个List中返回。具体实现思路如下: 1. 首先判断输入字符串是否为空或者长度为0,如果是则直接返回一个空的List。 2. 创建一个StringBuilder对象用于存储切分后的字符串。 3. 创建一个变量byteCount用于记录当前字符串的字节数。 4. 遍历输入字符串的每个字符,获取该字符的字节数。 5. 如果当前字符的字节数加上已有字符串的字节数小于等于30,则将该字符加入StringBuilder对象中,并更新byteCount。 6. 如果超过30个字节,则将之前的字符串添加到结果List中,并重新开始计数。 7. 最后将最后一个StringBuilder对象中的内容添加到结果List中。 8. 返回结果List。 可以看到,该函数通过遍历输入字符串的每个字符,并判断当前字符的字节数加上已有字符串的字节数是否超过30个字节,来实现按30个字节切分字符串的功能。对于一个字节长度大于1的字符,我们使用了一个辅助函数calculateByteSize来计算其字节数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值