zoukankan      html  css  js  c++  java
  • 集合Collection

     1 /*
     2  * 集合的由来:
     3  *         我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储。
     4  *         而要想存储多个对象,就不能是一个基本的变量,而应该是一个容器类型的变量,在我们目前所学过的知识里面,有哪些是容器类型的呢?
     5  *         数组和StringBuffer。但是呢?StringBuffer的结果是一个字符串,不一定满足我们的要求,所以我们只能选择数组,这就是对象数组。
     6  *         而对象数组又不能适应变化的需求,因为数组的长度是固定的,这个时候,为了适应变化的需求,Java就提供了集合类供我们使用。
     7  * 
     8  * 数组和集合的区别?
     9  *         A:长度区别
    10  *             数组的长度固定
    11  *             集合长度可变
    12  *         B:内容不同
    13  *             数组存储的是同一种类型的元素
    14  *             而集合可以存储不同类型的元素
    15  *         C:元素的数据类型问题    
    16  *             数组可以存储基本数据类型,也可以存储引用数据类型
    17  *             集合只能存储引用类型
    18  * 
    19  * 刚说过集合是存储多个元的,但是呢,存储多个元素我们也是有不同需求的:比如说,我要这多个元素中不能有相同的元素,
    20  * 再比如说,我要这多个元素按照某种规则排序一下。针对不同的需求,Java就提供了不同的集合类,这样呢,Java就提供了很多个集合类。
    21  * 这多个集合类的数据结构不同,结构不同不重要的,重要的是你要能够存储东西,并且还要能够使用这些东西,比如说判断,获取等。
    22  * 既然这样,那么,这多个集合类是有共性的内容的,我们把这些集合类的共性内容不断的向上提取,最终就能形成集合的继承体系结构。
    23  * 
    24  * 数据结构:数据的存储方式。
    25  * 
    26  * Collection:是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。(后面会慢慢的讲解)
    27  * 
    28  * Collection的功能概述:
    29  * 1:添加功能
    30  *         boolean add(Object obj):添加一个元素
    31  *         boolean addAll(Collection c):添加一个集合的元素
    32  * 2:删除功能
    33  *         void clear():移除所有元素
    34  *         boolean remove(Object o):移除一个元素
    35  *         boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
    36  * 3:判断功能
    37  *         boolean contains(Object o):判断集合中是否包含指定的元素
    38  *         boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
    39  *         boolean isEmpty():判断集合是否为空
    40  * 4:获取功能
    41  *         Iterator<E> iterator()(重点)
    42  * 5:长度功能
    43  *         int size():元素的个数
    44  *         面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
    45  * 6:交集功能
    46  *         boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
    47  * 7:把集合转换为数组
    48  *         Object[] toArray()
    49  */
    50 public class CollectionDemo {
    51     public static void main(String[] args) {
    52         // 测试不带All的方法
    53 
    54         // 创建集合对象
    55         // Collection c = new Collection(); //错误,因为接口不能实例化
    56         Collection c = new ArrayList();
    57 
    58         // boolean add(Object obj):添加一个元素
    59         // System.out.println("add:"+c.add("hello"));
    60         c.add("hello");
    61         c.add("world");
    62         c.add("java");
    63 
    64         // void clear():移除所有元素
    65         // c.clear();
    66 
    67         // boolean remove(Object o):移除一个元素
    68         // System.out.println("remove:" + c.remove("hello"));
    69         // System.out.println("remove:" + c.remove("javaee"));
    70 
    71         // boolean contains(Object o):判断集合中是否包含指定的元素
    72         // System.out.println("contains:"+c.contains("hello"));
    73         // System.out.println("contains:"+c.contains("android"));
    74 
    75         // boolean isEmpty():判断集合是否为空
    76         // System.out.println("isEmpty:"+c.isEmpty());
    77 
    78         //int size():元素的个数
    79         System.out.println("size:"+c.size());
    80         
    81         System.out.println("c:" + c);
    82     }
    83 }
     1 /*
     2  * boolean addAll(Collection c):添加一个集合的元素
     3  * boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
     4  * boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
     5  * boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
     6  */
     7 public class CollectionDemo2 {
     8     public static void main(String[] args) {
     9         // 创建集合1
    10         Collection c1 = new ArrayList();
    11         c1.add("abc1");
    12         c1.add("abc2");
    13         c1.add("abc3");
    14         c1.add("abc4");
    15 
    16         // 创建集合2
    17         Collection c2 = new ArrayList();
    18 //        c2.add("abc1");
    19 //        c2.add("abc2");
    20 //        c2.add("abc3");
    21 //        c2.add("abc4");
    22         c2.add("abc5");
    23         c2.add("abc6");
    24         c2.add("abc7");
    25 
    26         // boolean addAll(Collection c):添加一个集合的元素
    27         // System.out.println("addAll:" + c1.addAll(c2));
    28         
    29         //boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
    30         //只要有一个元素被移除了,就返回true。
    31         //System.out.println("removeAll:"+c1.removeAll(c2));
    32 
    33         //boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
    34         //只有包含所有的元素,才叫包含
    35         // System.out.println("containsAll:"+c1.containsAll(c2));
    36         
    37         //boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
    38         //假设有两个集合A,B。
    39         //A对B做交集,最终的结果保存在A中,B不变。
    40         //返回值表示的是A是否发生过改变。
    41         System.out.println("retainAll:"+c1.retainAll(c2));
    42         
    43         System.out.println("c1:" + c1);
    44         System.out.println("c2:" + c2);
    45     }
    46 }
     1 /*
     2  * 集合的遍历。其实就是依次获取集合中的每一个元素。
     3  * 
     4  * Object[] toArray():把集合转成数组,可以实现集合的遍历
     5  */
     6 public class CollectionDemo3 {
     7     public static void main(String[] args) {
     8         // 创建集合对象
     9         Collection c = new ArrayList();
    10 
    11         // 添加元素
    12         c.add("hello"); // Object obj = "hello"; 向上转型
    13         c.add("world");
    14         c.add("java");
    15 
    16         // 遍历
    17         // Object[] toArray():把集合转成数组,可以实现集合的遍历
    18         Object[] objs = c.toArray();
    19         for (int x = 0; x < objs.length; x++) {
    20             // System.out.println(objs[x]);
    21             // 我知道元素是字符串,我在获取到元素的的同时,还想知道元素的长度。
    22             // System.out.println(objs[x] + "---" + objs[x].length());
    23             // 上面的实现不了,原因是Object中没有length()方法
    24             // 我们要想使用字符串的方法,就必须把元素还原成字符串
    25             // 向下转型
    26             String s = (String) objs[x];
    27             System.out.println(s + "---" + s.length());
    28         }
    29     }
    30 }

    ===============================================

    Iterator iterator():迭代器,集合的专用遍历方式

     1 /*
     2  * Iterator iterator():迭代器,集合的专用遍历方式
     3  *         Object next():获取元素,并移动到下一个位置。
     4  *             NoSuchElementException:没有这样的元素,因为你已经找到最后了。
     5  *         boolean hasNext():如果仍有元素可以迭代,则返回 true。(
     6  */
     7 public class IteratorDemo {
     8     public static void main(String[] args) {
     9         // 创建集合对象
    10         Collection c = new ArrayList();
    11 
    12         // 创建并添加元素
    13         // String s = "hello";
    14         // c.add(s);
    15         c.add("hello");
    16         c.add("world");
    17         c.add("java");
    18 
    19         // Iterator iterator():迭代器,集合的专用遍历方式
    20         Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态
    21 
    22         // Object obj = it.next();
    23         // System.out.println(obj);
    24         // System.out.println(it.next());
    25         // System.out.println(it.next());
    26         // System.out.println(it.next());
    27         // System.out.println(it.next());
    28         // 最后一个不应该写,所以,我们应该在每次获取前,如果有一个判断就好了
    29         // 判断是否有下一个元素,有就获取,没有就不搭理它
    30 
    31         // if (it.hasNext()) {
    32         // System.out.println(it.next());
    33         // }
    34         // if (it.hasNext()) {
    35         // System.out.println(it.next());
    36         // }
    37         // if (it.hasNext()) {
    38         // System.out.println(it.next());
    39         // }
    40         // if (it.hasNext()) {
    41         // System.out.println(it.next());
    42         // }
    43         // if (it.hasNext()) {
    44         // System.out.println(it.next());
    45         // }
    46 
    47         // 最终版代码
    48         while (it.hasNext()) {
    49             // System.out.println(it.next());
    50             String s = (String) it.next();
    51             System.out.println(s);
    52         }
    53     }
    54 }
  • 相关阅读:
    微信二维码 场景二维码 用于推送事件,关注等 注册用户 ,经过测试
    简单的 Helper 封装 -- CookieHelper
    简单的 Helper 封装 -- SecurityHelper 安全助手:封装加密算法(MD5、SHA、HMAC、DES、RSA)
    Java反射机制
    Windows Azure Web Site (13) Azure Web Site备份
    Windows Azure Virtual Machine (1) IaaS用户手册
    Windows Azure Web Site (1) 用户手册
    Windows Azure Web Site (12) Azure Web Site配置文件
    Windows Azure Web Site (11) 使用源代码管理器管理Azure Web Site
    Windows Azure Web Site (10) Web Site测试环境
  • 原文地址:https://www.cnblogs.com/jiangjianzhu/p/5811214.html
Copyright © 2011-2022 走看看