List常用的操作

1、看List里是否存在某个元素 contains

复制代码
//省略建立list
 boolean contains = stringList.contains("上海");
 System.out.println(contains);

如果存在是true,不存在是false

2、看某个元素在List中的索引号 .indexOf

复制代码
List<String>stringList = new ArrayList<>();
        stringList.add("上海");
        stringList.add("厦门");
        int i = stringList.indexOf("上海");
        int j = stringList.indexOf("北京");
        System.out.println(i);
        System.out.println(j);

如果不存在,输出结果 -1 ,否则输出该元素在集合中是索引号。

3、数据库里某个字段 in (your_list)

复制代码
select * from t_class where id in  (your_list);

4、List空集合的size是0 若要判空,别用 size < 0 ,用.isEmpty()

复制代码
        List<String> newList = new ArrayList<>();
        boolean empty = newList.isEmpty();
        int size = newList.size();
        System.out.println(empty);
        System.out.println(size);

输出结果:

true

0

5、将指定集合添加到list结尾 listA.addAll(listB)

复制代码
        List<String>A = new ArrayList<>();
        A.add("上海");
        A.add("厦门");

        List<String>B = new ArrayList<>();
        B.add("北京");
        B.add("深圳");

        //addAll 成功为true
        boolean b = A.addAll(B);
        System.out.println(b);
        //此时A输出结果 [上海, 厦门, 北京, 深圳]
        System.out.println(A);
相关推荐
皮皮冰燃1 天前
关系数据库-10-[mysql5和mysql8]在windows中安装为服务并共存
windows·mysql
太空1号1 天前
VxWorks入门小白菜鸟教程3 —— 编译运行VxWorksSDK示例hello_cmake_rtp(Windows篇)
windows·嵌入式硬件
天朝八阿哥1 天前
Bye~~ win10!
linux·windows
孙同学_1 天前
【Linux篇】软链接vs硬链接:Linux文件系统中的两种引用机制
linux·运维·服务器
景彡先生1 天前
Python列表(List)完全指南:从入门到实战优化
windows·python·list
hour_go1 天前
解决Linux系统中“undeclared identifier“问题的完整指南
linux·运维·服务器
天赐细莲1 天前
(Linux) WSL 通过 VSCode 连接不执行 profile 问题(登录Shell问题)
linux·运维·vscode
咬_咬1 天前
Linux时间轮定时器
linux·运维·网络·定时器·timerfd
LCG元1 天前
Linux Shell脚本编程实战:自动备份网站文件和数据库,并定期清理过期备份
linux
Liu1bo1 天前
【MySQL】表的约束
linux·数据库·mysql