Java基础——常用对象API(4):集合框架2:List集合

1.List集合特点

有序(存入和取出的顺序一致);元素有索引(角标);元素可以重复
在这里插入图片描述


2.List集合方法

        List arrayList = new ArrayList();

        //添加元素
        arrayList.add("abc1");
        arrayList.add("abc2");
        arrayList.add("abc3");
        arrayList.add("abc4");
        System.out.println(arrayList);// [abc1, abc2, abc3, abc4]

        //插入元素
        arrayList.add(1,"abc5");
        System.out.println(arrayList);// [abc1, abc5, abc2, abc3, abc4]

        //删除元素
        System.out.println(arrayList.remove(1));//abc5
        System.out.println(arrayList);// [abc1, abc2, abc3, abc4]

        //修改元素
        System.out.println(arrayList.set(1,"abc9"));// abc2
        System.out.println(arrayList);// [abc1, abc9, abc3, abc4]

        //获取子列表
        System.out.println(arrayList.subList(1,3));// [abc9, abc3]

        //获取元素
        System.out.println(arrayList.get(3));// abc4
        System.out.println(arrayList.get(4));// IndexOutOfBoundsException

3. ListIterator(只有List集合具备)

3.1 问题引出

下述代码中,集合和迭代器同时在对元素进行修改,会产生异常。也就是说,在迭代器过程中,不要使用集合操作元素,容易出现异常。

		List l = new ArrayList();

        l.add("abc1");
        l.add("abc2");
        l.add("abc3");
        l.add("abc4");

        Iterator i = l.iterator();
        while(i.hasNext()) {

            Object obj = i.next();

            if(obj.equals("abc3")) {
                l.add("abc9");//ConcurrentModificationException
            }else{
                System.out.println(obj);
            }
        }

3.2 解决方法

可以使用Iterator的子接口ListIterator来完成在迭代中对元素进行操作。

        List l = new ArrayList();
        l.add("abc1");
        l.add("abc2");
        l.add("abc3");
        l.add("abc4");

        ListIterator i = l.listIterator();
        while(i.hasNext()) {

            Object obj = i.next();

            if(obj.equals("abc3")) {
                i.set("abc9");
            }else{
                System.out.println(obj);
            }
        }
        System.out.println(l);// [abc1, abc2, abc9, abc4]

3.3 ListIterator中的方法:

boolean hasPrevious():逆向遍历列表
boolean hasNext():正向遍历列表

previous():返回列表中的前一个元素
next():返回列表中的下一个元素


4 Vector,ArrayList和LinkedList

Vector:内部是数组数据结构,是同步的。增删,查询都很慢。
ArrayList:内部是数组数据结构,是不同步的。替代Vector。查询的速度快。
LinkedList:内部是链表数据结构,是不同步的。增删元素的速度很快。

4.1 Vector代码示例

        Vector v = new Vector();
        v.addElement("abc1");
        v.addElement("abc2");
        v.addElement("abc3");
        v.addElement("abc4");

        for(Enumeration enumeration = v.elements();enumeration.hasMoreElements(); ) {
            System.out.println(enumeration.nextElement());
        }

ps: Enumeration和Iterator的功能是重复的,都是遍历。

4.2 LinkedList代码示例

import java.util.LinkedList;

class Queue{ //用LinkedList做一个队列
    private LinkedList linkedList;

    Queue() {
        linkedList = new LinkedList();
    }

    public void myAdd(Object obj) {
        linkedList.addFirst(obj);
    }

    public Object myGet() {
        return linkedList.removeFirst();
    }

    public boolean isNull() {
        return linkedList.isEmpty();
    }
}

public class LinkedListDemo {
    public static void main(String[] args) {
        Queue queue = new Queue();
        queue.myAdd("abc1");
        queue.myAdd("abc2");
        queue.myAdd("abc3");

        while(!queue.isNull()) {
            System.out.println(queue.myGet());
        }
    }
}

4.3 ArrayList代码示例

import java.util.ArrayList;
import java.util.Iterator;

class Student {
        private String name;
        private int age;

        Student() {

        }

        Student(String name,int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }
    }

    public class ArrayListDemo {
        public static void main(String[] args) {
            ArrayList arrayList = new ArrayList();

            arrayList.add(new Student("abc1",21));
            arrayList.add(new Student("abc2",22));
            arrayList.add(new Student("abc3",23));
            arrayList.add(new Student("abc4",24));
            
            for(Iterator iterator = arrayList.iterator();iterator.hasNext();) {
                Student s = (Student)iterator.next();
                System.out.println(s.getName()+"—————"+s.getAge());
            }
        }
    }

5.定义功能去除ArrayList中的重复元素

首先,下面这段代码就是用迭代器遍历输出对象的name和age。

import java.util.ArrayList;
import java.util.Iterator;

class Student{
    private String name;
    private int age;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

public class Demo {
    public static void main(String[] args) {

        ArrayList a = new ArrayList();
        a.add(new Student("zxc1",21));
        a.add(new Student("zxc2",22));
        a.add(new Student("zxc2",22));
        a.add(new Student("zxc3",23));
        a.add(new Student("zxc3",23));

        Iterator iterator = a.iterator();
        while(iterator.hasNext()) {
            Student s = (Student)iterator.next();
            System.out.println(s.getName()+"——"+s.getAge());
        }
    }
}

输出发现有两个对象输出的值是一样的,所以通过增加功能删除元素值相同的对象。

解决方法:在Student类中重写equals方法,在Demo类中定义一个新的getSingle方法。

import java.util.ArrayList;
import java.util.Iterator;

class Student{
    private String name;
    private int age;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public boolean equals(Object obj) {
        Student s = (Student)obj;
        return s.age==this.age &&
                this.name==s.name;
    }
}

public class Demo {
    public static void main(String[] args) {

        ArrayList a = new ArrayList();
        a.add(new Student("zxc1",21));
        a.add(new Student("zxc2",22));
        a.add(new Student("zxc2",22));
        a.add(new Student("zxc3",23));
        a.add(new Student("zxc3",23));

        a = getSingle(a);

        Iterator iterator = a.iterator();
        while(iterator.hasNext()) {
            Student s = (Student)iterator.next();
            System.out.println(s.getName()+"——"+s.getAge());
        }
    }

    public static ArrayList getSingle(ArrayList a) {
        ArrayList temp = new ArrayList();

        Iterator iterator = a.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();

            if (!temp.contains(obj)) {
                temp.add(obj);
            }
        }

        return temp;
    }
}

重写equals方法是因为getSingle方法中的contains是根据equals来判断的,不改写之前,equals判断的是地址值,即使对象name和age相同的情况下地址值都是不一样的,因为它们都是new出来的对象,所以我们在Student类中将equals方法改写,改成只要name和age值相同就返回true。

6.遍历List集合的4个方法

  1. for (有索引,所以可以用for。但是Collection集合就不行,因为Collection集合没有索引)
  2. for each(增强for)
  3. 迭代器
  4. Lambda表达式

代码示例

import java.util.ArrayList;
import java.util.Iterator;

class Student {

    private String name;
    private int age;

    public Student(String name, int age) {
        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;
    }
}

public class Demo {
    public static void main(String[] args) {

        ArrayList<Student> a = new ArrayList<Student>();
        a.add(new Student("abc1",21));
        a.add(new Student("abc2",23));
        a.add(new Student("abc8",27));
        a.add(new Student("abc5",26));

        //  1.用for循环
        for (int i = 0; i < a.size(); i++) {
            Student s = a.get(i);
            System.out.println(s.getName()+":"+s.getAge());
        }

        System.out.println("————————————————————————");

        //  2.用迭代器
        for(Iterator<Student> iterator = a.iterator();iterator.hasNext();) {
            Student s = iterator.next();
            System.out.println(s.getName()+":"+s.getAge());
        }

        System.out.println("————————————————————————");

        //  3.用for each(增强for)循环
        for (Student s : a) {
            System.out.println(s.getName()+":"+s.getAge());
        }

        System.out.println("————————————————————————");

        //  4.Lambda表达式
        a.forEach(s->{
            System.out.println(s.getName()+":"+s.getAge());
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值