java 两个list_Java:将List分成两个子列表?

在Java中将List分成两个子列表的最简单,最标准和/或最有效的方法是什么?可以改变原始列表,因此不需要复制。方法签名可以是

/** Split a list into two sublists. The original list will be modified to

* have size i and will contain exactly the same elements at indices 0

* through i-1 as it had originally; the returned list will have size

* len-i (where len is the size of the original list before the call)

* and will have the same elements at indices 0 through len-(i+1) as

* the original list had at indices i through len-1.

*/

List split(List list, int i);

[EDIT] List.subList返回原始列表上的视图,如果原始视图被修改,则视图无效。所以split不能使用子列表,除非它也免除了原始引用(或者,如在Marc Novakowski的答案,使用subList,但立即复制结果)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
### 回答1: 可以使用 Java 中的 sublist 方法将 List分成多个 List。具体实现可以参考以下代码: ``` public static <T> List<List<T>> splitList(List<T> list, int size) { List<List<T>> result = new ArrayList<>(); int count = (list.size() + size - 1) / size; for (int i = ; i < count; i++) { int fromIndex = i * size; int toIndex = Math.min((i + 1) * size, list.size()); result.add(list.subList(fromIndex, toIndex)); } return result; } ``` 其中,list 表示要分割的 List,size 表示每个 List 的大小。该方法返回一个 List,其中每个元素都是一个 List。 ### 回答2: 在Java中,可以通过以下方式将List均分为多个列表。 首先,确定每个列表的大小,即每个列表中元素的个数。可以通过计算List的大小除以列表的个数来得到这个值。 然后,通过循环迭代的方式将List中的元素按照确定的每个列表的大小进行分割。可以使用List的subList方法来截取对应大小的列表。 下面是一个示例代码: ```java import java.util.ArrayList; import java.util.List; public class ListPartitioning { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); // 假设有一个大小为10的List for (int i = 1; i <= 10; i++) { list.add(i); } // 假设要将List分成3份 int partitionSize = list.size() / 3; List<List<Integer>> partitions = new ArrayList<>(); int fromIndex = 0; int toIndex = partitionSize; // 循环迭代分割List并添加到列表中 for (int i = 0; i < 3; i++) { if (i == 2) { toIndex = list.size(); // 最后一份可能不够分,将toIndex设为List的末尾 } List<Integer> partition = list.subList(fromIndex, toIndex); partitions.add(partition); fromIndex = toIndex; toIndex += partitionSize; } // 打印结果 for (List<Integer> partition : partitions) { System.out.println(partition); } } } ``` 以上代码将一个大小为10的List均分为3个列表,结果如下: [1, 2, 3] [4, 5, 6] [7, 8, 9, 10] ### 回答3: 要将一个List分成多个List,可以使用以下方法: 1. 获取原始List的大小,假设为totalSize。 2. 定义List的大小,假设为subListSize。 3. 计算需要分成List数量,假设为subListCount,可以使用Math.ceil(totalSize / subListSize)来得到向上取整的结果。 4. 创建一个新的ArrayList来存储所有List,假设为resultList。 5. 根据subListCount,循环subListCount次来创建List: 1. 计算当前List的起始索引,假设为start。 2. 计算当前List的结束索引,假设为end。当达到最后一个List时,结束索引为totalSize。 3. 使用List的subList方法,将原始List的[start, end)区间的元素作为一个新的List,并将其添加到resultList中。 6. 返回resultList作为分割好的List集合。 下面是一个示例代码: ```java import java.util.ArrayList; import java.util.List; public class ListPartition { public static List<List<Integer>> partitionList(List<Integer> originalList, int subListSize) { int totalSize = originalList.size(); int subListCount = (int) Math.ceil((double) totalSize / subListSize); List<List<Integer>> resultList = new ArrayList<>(); for (int i = 0; i < subListCount; i++) { int start = i * subListSize; int end = Math.min(start + subListSize, totalSize); List<Integer> subList = originalList.subList(start, end); resultList.add(subList); } return resultList; } public static void main(String[] args) { List<Integer> originalList = new ArrayList<>(); originalList.add(1); originalList.add(2); originalList.add(3); originalList.add(4); originalList.add(5); List<List<Integer>> partitionedList = partitionList(originalList, 2); for (List<Integer> subList : partitionedList) { System.out.println(subList); } } } ``` 以上示例代码将会把原始List [1, 2, 3, 4, 5] 分成两个List [1, 2] 和 [3, 4, 5],并打印出来。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值