JavaSE(08)(List集合)

测试vector类:
package zz.itheima.list;

import java.util.List;
import java.util.Vector;

public class TestVector {
    //测试vector类
    public static void main(String[] args) {
        //Vector (向量列表)底层数据结构是动态数组,线程安全,增删和查询都很慢,出事容量为10,增量为10.
        //是list结构的实现类
        Vector v = new Vector();

        //1.存储
// boolean add(Object o) 继承自Collection
        v.add("hello");
        v.add(100);
// boolean addAll(Collection c) 继承自Collection
// add (int index,Object o)在第几个位置添加什么数据
        v.add(1, "world");
        System.out.println(v);
// boolean addAll (int index,Collection c)

        //2.替换
// set(int index,Object o)把下标为index的数据替换为后面新的数据
        v.set(2, 200);
        System.out.println(v);

        //3.删除
// boolean remove(Object o) 继承自Collection
        v.remove("world");
        System.out.println(v);
// boolean removeAll(Collection c) 继承自Collection
// boolean retainAll(Collection c) 继承自Collection
// void clear() 继承自Collection
        v.clear();
        System.out.println(v);
// remove(int index)
        v.add(0, "你好!");
        System.out.println(v);
        v.remove(0);
        System.out.println(v);

        //4.判断
// boolean contains(Object o) 继承自Collection
        System.out.println(v.contains("asdf"));
// boolean containsAll(Collection c) 继承自Collection
// boolean isEmpty() 继承自Collection
        System.out.println(v.isEmpty());
// int indexOf(Object o)数据的下标
        System.out.println(v.indexOf("asdf"));//-1代表没有找到内容所以就没有所谓的下标
// int lastIndexOf(Object o)

        //5.获取
// int size() 继承自Collection
        v.add("这是增加用的测试数据");
        System.out.println(v.size());
        v.add("asdf");v.add("iop");v.add(new Dog());v.add("");v.add("");v.add("");v.add("");
        System.out.println(v.capacity());//出事容量为10,增量为10
// Object get(int index)目前只能使用Object类进行接收,需要向下转型
        for (int i = 0; i < v.size(); i++) {
            Object temp = v.get(i);
            System.out.println(temp);
        }
// List subList(int fromIndex,int lastIndex) 固定开始和结尾的子集合
        List vv = v.subList(1, 3);
        System.out.println("*************************");
        System.out.println(vv);
        System.out.println(vv.getClass());
// Object[] toArray() 继承自Collection
        Object[] o=v.toArray();
    }
}
运行结果:
[hello, world, 100]
[hello, world, 200]
[hello, 200]
[]
[你好!]
[]
false
true
-1
1
10
这是增加用的测试数据
asdf
iop
zz.itheima.list.Dog@600c199f
*************************
[asdf, iop]
class java.util.Collections$SynchronizedRandomAccessList
ArrayList 的测试类:
package zz.itheima.list;

import java.util.ArrayList;

public class TestArrayList {
    //ArrayList 的测试类(是List接口的实现类之一)存储一组允许重复、有序的对象
    public static void main(String[] args) {
        // 案例:现有一个存储了一组字符串的List,请去重(用两个集合)
        ArrayList al = new ArrayList();
        ArrayList newArray = new ArrayList();

        al.add("aaa");
        al.add("bbb");
        al.add("aaa");
        al.add("bbb");
        al.add("eee");

        for (int i = 0; i < al.size(); i++) {
            //取数据
            String temp = (String) al.get(i);
            if (!newArray.contains(temp)) {
                newArray.add(temp);
            }
        }
        System.out.println(newArray);
    }
}
运行结果:
[aaa, bbb, eee]
LinkedList的测试类:
package zz.itheima.list;

import java.util.ArrayList;
import java.util.LinkedList;

public class TestLinkedList {
    //LinkedList的测试类
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        ArrayList arrayList = new ArrayList();
        linkedList.add("abc");
        linkedList.addFirst("def");
        System.out.println(linkedList.get(0));
        System.out.println(linkedList.getFirst());
        //测试LinkedList和ArrayList的增删效率
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            linkedList.add(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("LinkedList用了:"+(end-begin)+"毫秒的时间");
// long begin = System.currentTimeMillis();
// for (int i = 0; i < 10000000; i++) {
// arrayList.add(i);
// }
// long end = System.currentTimeMillis();
// System.out.println("LinkedList用了:"+(end-begin)+"的时间");

        //经测试,Arraylist 的插入数据要比LinkedList的效率要高
    }
}
运行结果:
def
def
使用Vector增删改查
package zz.itheima.list;

import java.util.Vector;

public class Demo1 {

    public static void main(String[] args) {
        //练习:使用Vector实现对图书(书名、价格)的增、删(根据书名)、遍历输出和单个查询(书名)
        Vector bookVector = new Vector();
        //增
        bookVector.add(new Book("aaa", 100.9));
        bookVector.add(new Book("bbb", 101.9));
        bookVector.add(new Book("ccc", 102.9));
        bookVector.add(new Book("ddd", 103.9));
        bookVector.add(new Book("eee", 104.9));
        bookVector.add(new Book("fff", 105.9));
        System.out.println(bookVector);
        //删
        String keyName = "bbb";
        for (int i = 0; i < bookVector.size(); i++) {
            Book temp = (Book) bookVector.get(i);
            if (temp.getName().equals(keyName)) {
                bookVector.remove(i);
                break;
            }
        }
        System.out.println(bookVector);
        //遍历输出
        for (int i = 0; i < bookVector.size(); i++) {
            Book temp = (Book) bookVector.get(i);
            System.out.println(temp.getName());
        }
        //根据书名单个查询
        boolean flag = true;
        for (int i = 0; i < bookVector.size(); i++) {
            Book temp = (Book) bookVector.get(i);
            if (temp.getName().equals("aaa")) {
                System.out.println("找到了");
                flag = false;
                break;
            }
        }
        if (flag) {
            System.out.println("没有找到!");
        }
    }
}
运行结果:
[aaa:100.9, bbb:101.9, ccc:102.9, ddd:103.9, eee:104.9, fff:105.9]
[aaa:100.9, ccc:102.9, ddd:103.9, eee:104.9, fff:105.9]
aaa
ccc
ddd
eee
fff
找到了
集合去重:
package zz.itheima.list;

import java.util.ArrayList;

public class Demo2 {

    public static void main(String[] args) {
        // 练习:现有一个存储了一组对象的List,请去重(注意重写equals方法)
        ArrayList al=new ArrayList();
        ArrayList newAL=new ArrayList();    //空集合

        al.add(new Book("aaa", 10.08));
        al.add(new Book("bbb", 10.08));
        al.add(new Book("aaa", 10.08));

        for (int i = 0; i < al.size(); i++) {
            //1.取
            //2.判断
            //3.放还是不放
            Book temp=(Book)al.get(i);
            if(!newAL.contains(temp))
                newAL.add(temp);
        }
        System.out.println(newAL);
    }
}
运行结果:
[aaa:10.08, bbb:10.08]
TestContains的测试类:
package zz.itheima.list;

import java.util.ArrayList;
import java.util.List;

public class TestContains {
    //TestContains的测试类
    public static void main(String[] args) {
        // contains的内部实现是通过equals方法判断是否存在的
                // 自定义类中必须重写equals方法
        List bookList = new ArrayList();
        bookList.add(new Book("aaa",10.04));
        bookList.add(new Book("bbb",10.04));
        bookList.add(new Book("ccc",10.04));

        boolean result = bookList.contains(new Book("aaa", 10.04));
        System.out.println(result);
    }
}
运行结果:
true
bean类:
package zz.itheima.list;
public class Dog {
}
package zz.itheima.list;

public class Book {
    private String name;
    private double price;

    public Book(String name, double price) {
        super();
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String toString()
    {
        return name+":"+price;
    }

    public boolean equals(Object o){
        if(this==o)
            return true;
        else {
            if(o instanceof Book)
            {
                Book other=(Book)o;
                if(this.getName().equals(other.getName())&&this.getPrice()==other.getPrice())
                    return true;
                else {
                    return false;
                }
            }
            else {
                return false;
            }
        }
    }
}
书管理类:
package zz.itheima.list;

import java.util.ArrayList;
    /** * 书的管理类 * @author Administrator * */
public class BookMgr {
    private ArrayList bookList ;

    public BookMgr() {
        bookList = new ArrayList();
    }

    //添加
    public void addBook(String name,double price){
        bookList.add(new Book(name, price));
        System.out.println(bookList);
        System.out.println("添加成功!");
    }
    //删除
    public void delBook(String name){
        boolean flag = true;
        for (int i = 0; i < bookList.size(); i++) {
            Book temp = (Book) bookList.get(i);
            if (name.equals(temp.getName())) {
                bookList.remove(i);
                flag = false;
                System.out.println("删除成功");
                break;
            }
        }
        if (flag) {
            System.out.println("书不存在");
        }
        System.out.println(bookList);
    }
    //修改(根据名称修改价格)
    public void editPrice(String name,double newPrice){
        for (int i = 0; i < bookList.size(); i++) {
            Book temp = (Book) bookList.get(i);
            if (temp.getName().equals(name)) {
                temp.setPrice(newPrice);
            }
        }
        System.out.println("修改成功了");
        System.out.println(bookList);
    }
}
测试管理类:
package zz.itheima.list;

public class TestBookMgr {

    public static void main(String[] args) {
        BookMgr bm = new BookMgr();

        bm.addBook("aaa", 100);
        bm.addBook("bbb", 100);
        bm.addBook("ccc", 100);
        bm.addBook("ddd", 100);
        bm.addBook("eee", 100);

        bm.delBook("ddd");

        bm.editPrice("aaa", 200);
    }
}
运行结果:
[aaa:100.0]
添加成功!
[aaa:100.0, bbb:100.0]
添加成功!
[aaa:100.0, bbb:100.0, ccc:100.0]
添加成功!
[aaa:100.0, bbb:100.0, ccc:100.0, ddd:100.0]
添加成功!
[aaa:100.0, bbb:100.0, ccc:100.0, ddd:100.0, eee:100.0]
添加成功!
删除成功
[aaa:100.0, bbb:100.0, ccc:100.0, eee:100.0]
修改成功了
[aaa:200.0, bbb:100.0, ccc:100.0, eee:100.0]


































你可能感兴趣的:(List集合)