E get (int index) 通过索引获取元素
也可以用来遍历
E set (int index,E element) 修改指定位置的元素
输出
[a, d, c]
sublist(int startndex ,int endIndex)
获取下从startIndex到endIndex的元素
包括startIndex位置 不括号endIndex位置
注意判断索引是否越界
public class Test {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
List<Integer> result = gePageList(list,2,3);
System.out.println(result);
}
public static List<Integer> gePageList(List<Integer> list,int startIndex,int endIndex) {
if (startIndex > endIndex) {
throw new IllegalArgumentException("startIndex必须小于等于endIndex");
}
if (startIndex < 0) {
throw new IllegalArgumentException("startIndex必须大于等于0");
}
if (endIndex > list.size()) {
throw new IllegalArgumentException("endIndex超过了集合范围");
}
List<Integer> result = list.subList(startIndex,endIndex);
return result;
}
}
输出
[3]
-------------------------------
forEach() Lambda表达式
![在这里插入图片描述](https://img-blog.csdnimg.cn/a95653b598ea4f039826a05be9d1e168.png)
----------------------------------------------------------------
![在这里插入图片描述](https://img-blog.csdnimg.cn/7b709f77f6564c55a5a79e76860731c5.png)