温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Integer中怎么利用remove方法删除元素

发布时间:2021-07-27 15:10:46 来源:亿速云 阅读:117 作者:Leah 栏目:大数据

这期内容当中小编将会给大家带来有关Integer中怎么利用remove方法删除元素,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

调用类型是 Integer 的 ArrayList 的 remove 方法删除元素,传入一个 Integer 包装类的数字和传入一个 int 基本类型的数字,结果一样吗?

  • 传 int 基本类型的 remove 方法是按索引值移除,

    • 返回移除的值;

  • 传 Integer 包装类的 remove 方法是按值移除,

    • 返回列表移除项目之前是否包含这个值(是否移除成功)。

private static void removeByIndex(int index) {
    List<Integer> list =
            IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toCollection(ArrayList::new));
    System.out.println(list.remove(index));
    System.out.println(list);
}
private static void removeByValue(Integer index) {
    List<Integer> list =
            IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toCollection(ArrayList::new));
    System.out.println(list.remove(index));
    System.out.println(list);
}

测试一下 removeByIndex(4),通过输出可以看到第五项被移除了,返回 5:
5
[1, 2, 3, 4, 6, 7, 8, 9, 10]

而调用 removeByValue(Integer.valueOf(4)),通过输出可以看到值 4 被移除了,返回 true:
true
[1, 2, 3, 5, 6, 7, 8, 9, 10]

上述就是小编为大家分享的Integer中怎么利用remove方法删除元素了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI