Java_集合05_集合(List)的子类

集合(List)的子类

(1)    List的子类特点

ArrayList:

线性数据结构(底层数据结构是数组)。查询快,增删慢。线程不安全,效率高。创建集合时可以指定集合的长度。

Vector:

线性数据结构(底层数据结构是数组)。查询快,增删慢。线程安全,效率低。功能与ArrayList完全一致。

LinkedList:

链表数据结构(底层数据结构是链表)。查询慢,增删快。线程不安全,效率高。创建集合时不能指定集合的长度。

案例:使用List的任何子类存储字符串或者存储自定义对象并遍历。

(2)    ArrayList

        A:没有特有功能需要学习

        B:案例

            a:ArrayList存储字符串并遍历

            b:ArrayList存储自定义对象并遍历

/****************************************************************************************************************************************/

//ArrayList的使用:存储字符串并遍历

public class ArrayListDemo {

    public static void main(String[] args) {

        // 创建集合对象

        ArrayList array = new ArrayList();

 

        // 创建元素对象,并添加元素

        array.add("hello");

        array.add("world");

        array.add("java");

 

        // 遍历

        Iterator it = array.iterator();

        while (it.hasNext()) {

            String s = (String) it.next();

            System.out.println(s);

        }

 

        for (int x = 0; x < array.size(); x++) {

            String s = (String) array.get(x);

            System.out.println(s);

        }

    }

}

/****************************************************************************************************************************************/

//ArrayList的使用:存储自定义对象并遍历

public class ArrayListDemo {

    public static void main(String[] args) {

        // 创建集合对象

        ArrayList array = new ArrayList();

 

        // 创建学生对象

        Student s1 = new Student("武松", 30);

        Student s2 = new Student("鲁智深", 40);

        Student s3 = new Student("林冲", 36);

        Student s4 = new Student("杨志", 38);

 

        // 添加元素

        array.add(s1);

        array.add(s2);

        array.add(s3);

        array.add(s4);

 

        // 遍历

        Iterator it = array.iterator();

        while (it.hasNext()) {

            Student s = (Student) it.next();

            System.out.println(s.getName() + "---" + s.getAge());

        }

 

        for (int x = 0; x < array.size(); x++) {

            Student s = (Student) array.get(x);

            System.out.println(s.getName() + "---" + s.getAge());

        }

    }

}

public class Student {

    private String name;

    private int age;

    public Student() {}

    public Student(String name, int age) {

        super();

        this.name = name;

        this.age = age;

    }

    // getXxx()/setXxx()

    }

}

/****************************************************************************************************************************************/

(3)    Vector

        A:有特有功能

            a:添加

public void addElement(E obj):将指定元素添加到此向量的末尾。 -- public boolean add(E e)

            b:获取

public E elementAt(int index):返回指定索引处的组件。 -- public E get(int index)

public Enumeration<E> elements():返回此向量的组件的枚举。 -- iterator()

boolean hasMoreElements():测试此枚举是否包含更多的元素。

E 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);

        }

 

        Enumeration en = v.elements(); // 返回的是实现类的对象

        while (en.hasMoreElements()) {

            String s = (String) en.nextElement();

            System.out.println(s);

        }

    }

}

/****************************************************************************************************************************************/

        B:案例

            a:Vector存储字符串并遍历

            b:Vector存储自定义对象并遍历

(4)    LinkedList

        A:有特有功能    

            a:添加

public void addFirst(E e):将指定元素插入此列表的开头。

public void addLast(E e):将指定元素添加到此列表的结尾。

            b:获取

public E getFirst():返回此列表的第一个元素。

public E getLast():返回此列表的最后一个元素。

            c:删除

public E removeFirst():移除并返回此列表的第一个元素。

public E removeLast():移除并返回此列表的最后一个元素。

/****************************************************************************************************************************************/

public class LinkedListDemo {

    public static void main(String[] args) {

        // 创建集合对象

        LinkedList link = new LinkedList();

 

        // 添加元素

        link.add("hello");

        link.add("world");

        link.add("java");

 

        // public void addFirst(Object e)

        // link.addFirst("javaee");

        // public void addLast(Object e)

        // link.addLast("android");

 

        // public Object getFirst()

        // System.out.println("getFirst:" + link.getFirst());

        // public Obejct getLast()

        // System.out.println("getLast:" + link.getLast());

 

        // public Object removeFirst()

        System.out.println("removeFirst:" + link.removeFirst());// removeFirst:hello

        // public Object removeLast()

        System.out.println("removeLast:" + link.removeLast());// removeLast:java

 

        // 输出对象名

        System.out.println("link:" + link);// link:[world]

    }

}

/****************************************************************************************************************************************/

        B:案例

            a:LinkedList存储字符串并遍历

            b:LinkedList存储自定义对象并遍历

(5)    案例:

        A: ArrayList去除集合中的多个字符串的重复元素(如果字符串的内容相同,即为重复元素)

/****************************************************************************************************************************************/

/*

* 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);

        }

    }

}

/****************************************************************************************************************************************/

/*

* 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)

* 要求:不能创建新的集合,就在以前的集合上做。

*/

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");

 

        // 由选择排序思想引入,我们就可以通过这种思想做这个题目

        // 0索引的依次和后面的比较,有就把后的干掉

        // 同理,拿1索引...

        for (int x = 0; x < array.size() - 1; x++) {

            for (int y = x + 1; y < array.size(); y++) {

                if (array.get(x).equals(array.get(y))) {

                    array.remove(y);

                    y--;

                }

            }

        }

 

        // 遍历集合

        Iterator it = array.iterator();

        while (it.hasNext()) {

            String s = (String) it.next();

            System.out.println(s);

        }

    }

}

/****************************************************************************************************************************************/

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

/****************************************************************************************************************************************/

/*

* 我们按照和字符串一样的操作,发现出问题了。

* 为什么呢?

*         我们必须思考哪里会出问题?

*         通过简单的分析,我们知道问题出现在了判断上。

*         而这个判断功能是集合自己提供的,所以我们如果想很清楚的知道它是如何判断的,就应该去看源码。

* contains()方法的底层依赖的是equals()方法。

* 而我们的学生类中没有equals()方法,这个时候,默认使用的是它父亲Objectequals()方法

* Object()equals()默认比较的是地址值,所以,它们进去了。因为new的东西,地址值都不同。

* 按照我们自己的需求,比较成员变量的值,重写equals()即可。

* 自动生成即可。

*/

public class ArrayListDemo {

    public static void main(String[] args) {

        // 创建集合对象

        ArrayList array = new ArrayList();

 

        // 创建学生对象

        Student s1 = new Student("林青霞", 27);

        Student s2 = new Student("林志玲", 40);

        Student s3 = new Student("凤姐", 35);

        Student s4 = new Student("芙蓉姐姐", 18);

        Student s5 = new Student("翠花", 16);

        Student s6 = new Student("林青霞", 27);

        Student s7 = new Student("林青霞", 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() {}

    public Student(String name, int age) {

        super();

        this.name = name;

        this.age = age;

    }

    // getXxx()/setXxx()

}

/****************************************************************************************************************************************/

        C:用LinkedList模拟一个栈数据结构的集合类,并测试。

            你要定义一个集合类,只不过内部可以使用LinkedList来实现。

/****************************************************************************************************************************************/

//MyStackList的测试

public class MyStackListDemo {

    public static void main(String[] args) {

        // 创建集合对象

        MyStackList msl = new MyStackList();

 

        // 添加元素

        msl.add("hello");

        msl.add("world");

        msl.add("java");

 

        // System.out.println(msl.get());

        // System.out.println(msl.get());

        // System.out.println(msl.get());

        // System.out.println(msl.get());// NoSuchElementException

        

        while(!msl.isEmpty()){

            System.out.println(msl.get());

        }

    }

}

/***********************/

//自定义的栈集合类

public class MyStackList {

    private LinkedList link;

 

    public MyStackList() {

        link = new LinkedList();

    }

 

    public void add(Object obj) {

        link.addFirst(obj);

    }

 

    public Object get() {

        // return link.getFirst();

        return link.removeFirst();// return link.remove();效果是一样的

    }

 

    public boolean isEmpty() {

        return link.isEmpty();

    }

}

/****************************************************************************************************************************************/

 

 

 

猜你喜欢

转载自www.cnblogs.com/zhaolanqi/p/9254643.html