java学习_集合(1)

来源:互联网 发布:c语言实训步骤怎么写 编辑:程序博客网 时间:2024/06/14 21:42

1、对象数组

        (1)数组既可以存储基本数据类型,也可以存储引用类型。它存储引用类型的时候的数组就叫对象数组。
        (2)案例:
                    用数组存储5个学生对象,并遍历数组。

/** 分析: * A:创建学生类。 * B:创建学生数组(对象数组)。 * C:创建5个学生对象,并赋值。 * D:把C步骤的元素,放到数组中。 * E:遍历学生数组。 */public class ObjectArrayDemo {public static void main(String[] args) {// 创建学生数组(对象数组)。Student[] students = new Student[5];// for (int x = 0; x < students.length; x++) {// System.out.println(students[x]);// }// System.out.println("---------------------");// 创建5个学生对象,并赋值。Student s1 = new Student("xiaoa",11);Student s2 = new Student("xiaob", 12);Student s3 = new Student("xiaoc", 11);Student s4 = new Student("xiaod", 10);Student s5 = new Student("xiaoe", 13);// 把C步骤的元素,放到数组中。students[0] = s1;students[1] = s2;students[2] = s3;students[3] = s4;students[4] = s5;// 看到很相似,就想循环改// for (int x = 0; x < students.length; x++) {// students[x] = s + "" + (x + 1);// }// 这个是有问题的// 遍历for (int x = 0; x < students.length; x++) {//System.out.println(students[x]);Student s = students[x];System.out.println(s.getName()+"---"+s.getAge());}}}public class Student {// 成员变量private String name;private int age;// 构造方法public Student() {super();}public Student(String name, int age) {super();this.name = name;this.age = age;}// 成员方法// getXxx()/setXxx()public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}}

2、集合(Collection)
        (1)集合的由来?
我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer) -- 数组
而数组的长度固定,所以不适合做变化的需求,Java就提供了集合供我们使用。
        (2)集合和数组的区别
                A:长度区别
                        数组固定
                        集合可变
                B:内容区别
                        数组可以是基本类型,也可以是引用类型
                        集合只能是引用类型
                C:元素内容
                        数组只能存储同一种类型
                        集合可以存储不同类型(其实集合一般存储的也是同一种类型)
        (3)集合的继承体系结构
                由于需求不同,Java就提供了不同的集合类。这多个集合类的数据结构不同,但是它们都是要提供存储和遍历功能的,
                我们把它们的共性不断的向上提取,最终就形成了集合的继承体系结构图。
                Collection
                        |--List
                                |--ArrayList
                                |--Vector
                                |--LinkedList
                        |--Set
                                |--HashSet
                                |--TreeSet
          (4)Collection的功能概述
                A:添加功能

                        boolean add(Object obj):添加一个元素

                        boolean addAll(Collection c) :添加一个集合元素

                B:删除功能

                        void clear():移除此 collection 中的所有元素

                        boolean remove(Object o):  移除指定元素

                        boolean removeAll(Collection  c):移除集合的一个元素
                C:判断功能

                        boolean contains(Object o)   如果集合中包含指定的元素,则返回 true

                        boolean containsAll(Collection c)    如果集中中包含指定集合中的所有元素,则返回 true

                        boolean equals(Object o)     比较 集合与指定对象是否相等

                        boolean isEmpty()  判断集合是否为空

                 D:获取功能

                         Iterator<E> iterator()

                         返回在此 collection 的元素上进行迭代的迭代器

                         关于元素返回的顺序没有任何保证(除非此 collection 是某个能提供保证顺序的类实例)
                 E:长度功能

                         int size()   返回此 collection 中的元素数
                 F:交集

                        boolean retainAll(Collection<?> c)

                        仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)

                        换句话说,移除此 collection 中未包含在指定 collection 中的所有元素。

                        (假设有两个集合A、B,A对B做交集,最终结果保存在A中,B不变,

                          返回值表示的是A是否发生或改变)
                  G:把集合转数组

                        Object[] toArray()   返回包含此 collection 中所有元素的数组。

        (5)Collection集合的遍历
                A:把集合转数组,利用Object[] toArray() 
                B:迭代器(集合专用方式)
        (6)迭代器
                A:是集合的获取元素的方式。
                B:是依赖于集合而存在的。
         (7)Collection集合的案例(遍历方式 迭代器)
               集合的操作步骤:
                        A:创建集合对象
                        B:创建元素对象
                        C:把元素添加到集合
                        D:遍历集合
                A:存储字符串并遍历

                        import java.util.Collection;import java.util.ArrayList;import java.util.Iterator;public class CollectionDemo {public static void main(String[] args) {//创建集合对象Collection c = new ArrayList();//创建并添加元素c.add("hello");c.add("world");c.add("java");//遍历集合Iterator it = c.iterator();while(it.hasNext()) {String s =(String) it.next();System.out.println(s);}}}
                   B:存储自定义对象并遍历

import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/* * 练习:用集合存储5个学生对象,并把学生对象进行遍历。用迭代器遍历。 *  * 注意: * A:自己的类名不要和我们学习的要使用的API中的类名相同。 * B:复制代码的时候,很容易把那个类所在的包也导入过来,容易出现不能理解的问题。 */public class IteratorTest {public static void main(String[] args) {// 创建集合对象Collection c = new ArrayList();// 创建学生对象Student s1 = new Student("aa", 27);Student s2 = new Student("bb", 30);Student s3 = new Student("cc", 33);Student s4 = new Student("dd", 25);Student s5 = new Student("ee", 22);// 把学生添加到集合中c.add(s1);c.add(s2);c.add(s3);c.add(s4);c.add(s5);// 遍历Iterator it = c.iterator();while (it.hasNext()) {// System.out.println(it.next());Student s = (Student) it.next();System.out.println(s.getName() + "---" + s.getAge());}}}public class Student {// 成员变量private String name;private int age;// 构造方法public Student() {super();}public Student(String name, int age) {super();this.name = name;this.age = age;}// 成员方法// getXxx()/setXxx()public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}}

3、集合(List)
        (1)List是Collection的子接口
               特点:有序(存储顺序和取出顺序一致),可重复。
        (2)List的特有功能:
                A:添加功能

                            void add(int index,Object element)  在列表的指定位置插入指定元素。

                B:删除功能

                            Object remove(int index)   移除列表中指定位置的元素
                C:获取功能

                            Object get(int index)   返回列表中指定位置的元素
                D:迭代器功能

                             ListIterator listIterator()  返回此列表元素的列表迭代器(按适当顺序)。 

                E:修改功能

                             Object set(int index,Object element)    用指定元素替换列表中指定位置的元素

          (3)List集合的特有遍历功能
                A:由size()和get()结合。
                B:代码演示

                                       //创建集合对象List list = new ArrayList();//创建并添加元素list.add("hello");list.add("world");list.add("java");//遍历集合Iterator it = list.iterator();while(it.hasNext()) {String s =(String) it.next();System.out.println(s);}System.out.println("----------");for(int x=0; x<list.size(); x++) {String s =(String) list.get(x);System.out.println(s);}
        (4)列表迭代器的特有功能
                可以逆向遍历,但是要先正向遍历,所以无意义,基本不使用。
         (5)并发修改异常
                A:出现的现象
                        迭代器遍历集合,集合修改集合元素
                B:原因
                       迭代器是依赖于集合的,而集合的改变迭代器并不知道。
                C:解决方案
                        a:迭代器遍历,迭代器修改(ListIterator)
                                元素添加在刚才迭代的位置
                        b:集合遍历,集合修改(size()和get())
                                元素添加在集合的末尾
          (6)常见数据结构
                A:栈 先进后出
                B:队列 先进先出
                C:数组 查询快,增删慢
                D:链表 查询慢,增删快
           (7)List的子类特点
                ArrayList
                        底层数据结构是数组,查询快,增删慢。
                        线程不安全,效率高。
                Vector
                        底层数据结构是数组,查询快,增删慢。
                        线程安全,效率低。
LinkedList
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。

                到底使用谁呢?看需求?
                分析:
                         要安全吗?
                                要:Vector(即使要,也不使用这个,后面再说)
                                不要:ArrayList或者LinkedList
                                查询多;ArrayList
                               增删多:LinkedList
                  什么都不知道,就用ArrayList。

4、ArrayList

        ArrayList功能没有什么特殊的,前面Collection和List能用的它都适用

5、Vector
       A:有特有功能
                a:添加
                         public void addElement(E obj)

                b:获取
                        public E elementAt(int index)

                        public Enumeration<E> elements()
        B:案例 存储字符串并遍历

import java.util.Enumeration;import java.util.Vector;/* * Vector的特有功能: * 1:添加功能 * public void addElement(Object obj)       * 2:获取功能 * public Object elementAt(int index)        get() * public Enumeration elements() * boolean hasMoreElements() * Object nextElement() */public class VectorDemo {public static void main(String[] args) {// 创建集合对象Vector v = new Vector();// 添加功能v.addElement("hello");v.addElement("world");v.addElement("java");// 遍历for (int x = 0; x < v.size(); x++) {String s = (String) v.elementAt(x);System.out.println(s);}System.out.println("------------------");Enumeration en = v.elements(); // 返回的是实现类的对象while (en.hasMoreElements()) {String s = (String) en.nextElement();System.out.println(s);}}}
6、LinkedList
            有特有功能
                        a:添加
                                addFirst()
                                addLast()
                        b:删除
                                removeFirst()
                                removeLast()
                        c:获取
                                getFirst()
                                getLast()
7、案例:
                        A:去除集合中的多个字符串的重复元素
                         如果字符串的内容相同,即为重复元素

import java.util.ArrayList;import java.util.Iterator;/* * ArrayList去除集合中字符串的重复值(字符串的内容相同) *  * 分析: * A:创建集合对象 * B:添加多个字符串元素(包含内容相同的) * C:创建新集合 * D:遍历旧集合,获取得到每一个元素 * E:拿这个元素到新集合去找,看有没有 * 有:不搭理它 * 没有:就添加到新集合 * F:遍历新集合 */public class ArrayListDemo {public static void main(String[] args) {// 创建集合对象ArrayList array = new ArrayList();// 添加多个字符串元素(包含内容相同的)array.add("hello");array.add("world");array.add("java");array.add("world");array.add("java");array.add("world");array.add("world");array.add("world");array.add("world");array.add("java");array.add("world");// 创建新集合ArrayList newArray = new ArrayList();// 遍历旧集合,获取得到每一个元素Iterator it = array.iterator();while (it.hasNext()) {String s = (String) it.next();// 拿这个元素到新集合去找,看有没有if (!newArray.contains(s)) {newArray.add(s);}}// 遍历新集合for (int x = 0; x < newArray.size(); x++) {String s = (String) newArray.get(x);System.out.println(s);}}}

                        B:去除集合中的多个自定义对象的重复元素
                        如果自定义对象的成员变量值都相同,即为重复元素

import java.util.ArrayList;import java.util.Iterator;/* * 需求:去除集合中自定义对象的重复值(对象的成员变量值都相同) *  * 我们按照和字符串一样的操作,发现出问题了。 * 为什么呢? * 我们必须思考哪里会出问题? * 通过简单的分析,我们知道问题出现在了判断上。 * 而这个判断功能是集合自己提供的,所以我们如果想很清楚的知道它是如何判断的,就应该去看源码。 * contains()方法的底层依赖的是equals()方法。 * 而我们的学生类中没有equals()方法,这个时候,默认使用的是它父亲Object的equals()方法 * Object()的equals()默认比较的是地址值,所以,它们进去了。因为new的东西,地址值都不同。 * 按照我们自己的需求,比较成员变量的值,重写equals()即可。 * 自动生成即可。 */public class ArrayListDemo3 {public static void main(String[] args) {// 创建集合对象ArrayList array = new ArrayList();// 创建学生对象Student s1 = new Student("aa", 27);Student s2 = new Student("bb", 40);Student s3 = new Student("cc", 35);Student s4 = new Student("dd", 18);Student s5 = new Student("ee", 16);Student s6 = new Student("aa", 27);Student s7 = new Student("aa", 18);// 添加元素array.add(s1);array.add(s2);array.add(s3);array.add(s4);array.add(s5);array.add(s6);array.add(s7);// 创建新集合ArrayList newArray = new ArrayList();// 遍历旧集合,获取得到每一个元素Iterator it = array.iterator();while (it.hasNext()) {Student s = (Student) it.next();// 拿这个元素到新集合去找,看有没有if (!newArray.contains(s)) {newArray.add(s);}}// 遍历新集合for (int x = 0; x < newArray.size(); x++) {Student s = (Student) newArray.get(x);System.out.println(s.getName() + "---" + s.getAge());}}}public class Student {private String name;private int age;public Student() {super();}public Student(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}}

解决问题思路:

    A:分析,从具体到抽象

    B:实现,从抽象到具体

    C:使用,使用具体的

0 0
原创粉丝点击