通过指定索引从 ArrayList 中检索元素 [英] Retrieving elemnts from an ArrayList by specifying the indexes

查看:36
本文介绍了通过指定索引从 ArrayList 中检索元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 中是否有一种方法可以通过指定开始和结束索引来将对象列表从一个 Arraylist 获取到另一个 ArrayList?

Is there a method in Java to get the list of objects from an Arraylist to another ArrayList, by just specifying the start and end index?

推荐答案

是的,您可以使用 subList 方法:

Yes you can use the subList method:

List<...> list2 = list1.subList(startIndex, endIndex);

这将返回原始列表那部分的视图,它不会复制数据.
如果你想要一个副本:

This returns a view on that part of the original list, it does not copy the data.
If you want a copy:

List<...> list2 = new ArrayList<...> (list1.subList(startIndex, endIndex));

这篇关于通过指定索引从 ArrayList 中检索元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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