ToB企服应用市场:ToB评测及商务社交产业平台

标题: 1-集合先容 [打印本页]

作者: 惊雷无声    时间: 2024-7-11 22:36
标题: 1-集合先容
1-集合先容

先容汇总:
1-为啥要使用集合

2-集合的框架体系

3-Collection方法

  1.         // 创建一个类型为数组列表的列表对象
  2.         List list = new ArrayList();
  3.         // 添加单个元素
  4.         list.add("吴彦祖");
  5.         list.add("彭于晏");
  6.         list.add("杨洋");
  7.         list.add(89586);
  8.         System.out.println("====列表中的元素有" + list + "====");
  9.         // 删除指定元素
  10.         list.remove("吴彦祖");
  11.         System.out.println("====列表中的元素有" + list + "====");
  12.         // 查找某个元素是否被存在
  13.         boolean result = list.contains("吴彦祖");
  14.         if (result) {
  15.             System.out.println("====目标元素存在====");
  16.         } else {
  17.             System.out.println("====目标元素不存在====");
  18.         }
  19.         // 获取元素个数
  20.         System.out.println("====列表中元素个数为" + list.size());
  21.         // 判断列表是否为空
  22.         if (list.isEmpty()) {
  23.             System.out.println("====列表为空====");
  24.         } else {
  25.             System.out.println("====列表不为空====");
  26.         }
  27.         // 清空列表中的数据元素
  28.         list.clear();
  29.         if (list.isEmpty()) {
  30.             System.out.println("====列表为空====");
  31.         } else {
  32.             System.out.println("====列表不为空====");
  33.         }
  34.         // 列表中添加多个元素
  35.         ArrayList arrayList = new ArrayList();
  36.         arrayList.add("水浒传");
  37.         arrayList.add("三国演义");
  38.         arrayList.add("红楼梦");
  39.         list.addAll(arrayList);
  40.         System.out.println("====列表中的元素有" + list + "====");
  41.         // 查找多个元素是否都存在
  42.         list.add("西游记");
  43.         boolean containsedAll = list.containsAll(arrayList);
  44.         if (containsedAll) {
  45.             System.out.println("====目标元素存在====");
  46.         } else {
  47.             System.out.println("====目标元素不存在====");
  48.         }
  49.         // 删除多个元素
  50.         arrayList.remove("红楼梦");
  51.         list.removeAll(arrayList);
  52.         System.out.println("====列表中的元素有" + list + "====");
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4