集合2

1:List的子类(掌握)
(1)List的子类特点
ArrayList:
底层数据结构是数组,查询快,增删慢
线程不安全,效率高
Vector:
底层数据结构是数组,查询快,增删慢
线程安全,效率低
LinkedList:
底层数据结构是链表,查询慢,增删快
线程不安全,效率高
(2)ArrayList
A:没有特有功能需要学习
B:案例
a:ArrayList存储字符串并遍历
b:ArrayList存储自定义对象并遍历
(3)Vector
A:有特有功能
a:添加
public void addElement(E obj) – add()
b:获取
public E elementAt(int index) – get()
public Enumeration elements() – iterator()
B:案例
a:Vector存储字符串并遍历
b:Vector存储自定义对象并遍历
(4)LinkedList
A:有特有功能
a:添加
addFirst()
addLast()
b:删除
removeFirst()
removeLast()
c:获取
getFirst()
getLast()

/*
 * LinkedList的特有功能:
 *      A:添加功能
 *          public void addFirst(Object e)
 *          public void addLast(Object e)
 *      B:获取功能
 *          public Object getFirst()
 *          public Obejct getLast()
 *      C:删除功能
 *          public Object removeFirst()
 *          public Object 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());
        // public Object removeLast()
        System.out.println("removeLast:" + link.removeLast());

        // 输出对象名
        System.out.println("link:" + link);
    }
}
    B:案例
        a:LinkedList存储字符串并遍历
/*
 * ArrayList存储字符串并遍历
 * 
 * 我们按照正常的写法来写这个程序, 结果确出错了。
 * 为什么呢?
 *      因为我们开始存储的时候,存储了String和Integer两种类型的数据。
 *      而在遍历的时候,我们把它们都当作String类型处理的,做了转换,所以就报错了。
 * 但是呢,它在编译期间却没有告诉我们。
 * 所以,我就觉得这个设计的不好。
 * 回想一下,我们的数组
 *      String[] strArray = new String[3];
 *      strArray[0] = "hello";
 *      strArray[1] = "world";
 *      strArray[2] = 10;
 * 集合也模仿着数组的这种做法,在创建对象的时候明确元素的数据类型。这样就不会在有问题了。
 * 而这种技术被称为:泛型。
 * 
 * 泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型。参数化类型,把类型当作参数一样的传递。
 * 格式:
 *      <数据类型>
 *      此处的数据类型只能是引用类型。
 * 好处:
 *      A:把运行时期的问题提前到了编译期间
 *      B:避免了强制类型转换
 *      C:优化了程序设计,解决了黄色警告线
 */
public class GenericDemo {
    public static void main(String[] args) {
        // 创建
        ArrayList<String> array = new ArrayList<String>();

        // 添加元素
        array.add("hello");
        array.add("world");
        array.add("java");
        // array.add(new Integer(100));
        //array.add(10); // JDK5以后的自动装箱
        // 等价于:array.add(Integer.valueOf(10));

        // 遍历
        Iterator<String> it = array.iterator();
        while (it.hasNext()) {
            // ClassCastException
            // String s = (String) it.next();
            String s = it.next();
            System.out.println(s);
        }

        // 看下面这个代码
        // String[] strArray = new String[3];
        // strArray[0] = "hello";
        // strArray[1] = "world";
        // strArray[2] = 10;
    }
}
        b:LinkedList存储自定义对象并遍历
/*
 * 需求:存储自定义对象并遍历。
 * 
 * A:创建学生类
 * B:创建集合对象
 * C:创建元素对象
 * D:把元素添加到集合
 * E:遍历集合
 */
public class ArrayListDemo2 {
    public static void main(String[] args) {
        // 创建集合对象
        // JDK7的新特性:泛型推断。
        // ArrayList<Student> array = new ArrayList<>();
        // 但是我不建议这样使用。
        ArrayList<Student> array = new ArrayList<Student>();

        // 创建元素对象
        Student s1 = new Student("曹操", 40); // 后知后觉
        Student s2 = new Student("蒋干", 30); // 不知不觉
        Student s3 = new Student("诸葛亮", 26);// 先知先觉

        // 添加元素
        array.add(s1);
        array.add(s2);
        array.add(s3);

        // 遍历
        Iterator<Student> it = array.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getName() + "---" + s.getAge());
        }
        System.out.println("------------------");

        for (int x = 0; x < array.size(); x++) {
            Student s = array.get(x);
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}
(5)案例:
    A:去除集合中的多个字符串的重复元素
        如果字符串的内容相同,即为重复元素
/*
 * 需求:去除集合中自定义对象的重复值(对象的成员变量值都相同)
 * 
 * 我们按照和字符串一样的操作,发现出问题了。
 * 为什么呢?
 *      我们必须思考哪里会出问题?
 *      通过简单的分析,我们知道问题出现在了判断上。
 *      而这个判断功能是集合自己提供的,所以我们如果想很清楚的知道它是如何判断的,就应该去看源码。
 * 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("林青霞", 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());
        }
    }
}
    B:去除集合中的多个自定义对象的重复元素
        如果自定义对象的成员变量值都相同,即为重复元素
    C:用LinkedList模拟一个栈数据结构的集合类,并测试。
        你要定义一个集合类,只不过内部可以使用LinkedList来实现。
/**
 * 自定义的栈集合
 * 
 * @author 
 * @version V1.0
 */
public class MyStack {
    private LinkedList link;

    public MyStack() {
        link = new LinkedList();
    }

    public void add(Object obj) {
        link.addFirst(obj);
    }

    public Object get() {
        // return link.getFirst();
        return link.removeFirst();
    }

    public boolean isEmpty() {
        return link.isEmpty();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值