- 使用List子接口验证Collection接口中所提供的操作方法
- 掌握List子接口的操作特点以及常用子类(ArrayList,Vector)
List子接口(80%)是Collection最为常用的一个子接口。但是这个接口对Collection接口进行了一些功能的扩充。
在List子接口里面重点掌握以下方法的使用:
public E get(int index):取得索引编号的内容
public E set(int index,E element):修改指定索引的内容
public ListIterator<E> listIterator():为ListIterator接口实例化。
其中get方法比较常用。
List本身是属于接口,所以如果要想使用此接口进行操作,那么就必须存在有子类,可以使用ArrayList子类实现操作(还有另外一个Vector子类,90%情况下选择的是ArrayList)
2.1新的子类:ArrayList(非线程安全,基于对象数组实现,可扩容的动态数组,扩容时1.8以后使用位运算符扩容1.5倍)
ArrayList类是List接口最为常用的一个子类,下面将利用此类来验证所学习到的操作方法。
范例:List基本操作
public class TestDemo{
public static void main(String[] args) throws Exception{
//设置泛型,保证集合中所有的数据类型都一致
List<String> all=new ArrayList<String>();//这里没有指定初始长度 默认为10 超过10则开始被动扩容
System.out.println("length:"+all.size()+" is empty:"+all.isEmpty());
all.add("Hello");
all.add("Hello");//重复元素
all.add("Hello");
System.out.println("length:"+all.size()+" is empty:"+all.isEmpty());
//Collection接口定义了size方法可以取得集合长度
//List子接口扩充了一个get方法,可以根据索引取得数据
for (int i = 0; i < all.size(); i++) {
String str=all.get(i);
System.out.println(str);
}
}
}
结果:
通过演示可以发现,List集合之中所保存的数据是按照保存的顺序存放,而且允许重复,List子接口扩充有get方法。
范例:为Collection接口实例化(List子接口才有get方法)(仅为展示 不推荐使用)
- ArrayList是List接口子类,而List接口是Collection的子接口,自然可以通过ArrayList为Collection接口实例化。
public class TestDemo{
public static void main(String[] args) throws Exception{
//设置泛型,保证集合中所有的数据类型都一致
Collection<String> all=new ArrayList<String>();//这里没有指定初始长度 默认为10 超过10则开始被动扩容
System.out.println("length:"+all.size()+" is empty:"+all.isEmpty());
all.add("Hello");
all.add("Hello");//重复元素
all.add("Hello");
System.out.println("length:"+all.size()+" is empty:"+all.isEmpty());
Object obj[]=all.toArray();//变为对象数组取得
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
}
}
}
将Collection接口实现对象化为对象数组即可。
Collection接口与List接口相比,功能会显得有所不足,并且以上输出操作并不是集合会使用的结构,这只是一个基础展示。
范例:在集合里面保存对象
class Book{
private String title;
private double price;
public Book(String title,double price){
this.title=title;
this.price=price;
}
@Override
public String toString() {
return "Name:"+this.title+"price:"+this.price;
}
@Override
public boolean equals(Object obj) {//对象比较的方法
if(this==obj){
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof Book)){
return false;
}
Book book=(Book) obj;//向下转型
if(this.title.equals(book.title)&&this.price==book.price){
return true;
}
return false;
}
}
public class TestDemo{
public static void main(String[] args) throws Exception{
//设置泛型,保证集合中所有的数据类型都一致
List<Book> all=new ArrayList<Book>();//这里没有指定初始长度 默认为10 超过10则开始被动扩容
System.out.println("length:"+all.size()+" is empty:"+all.isEmpty());
all.add(new Book("Java",20.8));
all.add(new Book("Android",20.1));//重复元素
all.add(new Book(".NET",30.7));
System.out.println("length:"+all.size()+" is empty:"+all.isEmpty());
//任何情况下,集合数据的删除与内容的查询都必须提供有equals方法
all.remove(new Book(".NET",30.7));
System.out.println(all);
}
}
结果:
与之前的链表相比,几乎是横向替代就是替换了一个类名称而已,因为给出的链表就是按照Collection与List接口的方法标准定义的。
旧的子类:Vector
在最早JDK1.0的时候就已经提供有Vector类,并且这个类被大量的使用。但是到了JDK1.2的时候,由于类集框架的引入,对于整个集合的操作,有了新的标准,那么为了保留下Vector类,所以让这个类多实现了一个List接口。
范例:使用Vector
范例同第一个代码,将ArrayList改为Vector即可。因为只是实现子类的不同,调用的依旧是List接口的方法。
面试题:请解释ArrayList与Vector的区别?
- ArrayList (90%)
- JDK1.2推出
- 性能:采用异步处理,非线程安全
- 输出:Iterator,ListIterator,foreach
- Vector(10%)
- JDK1.0推出
- 性能:采用同步处理,线程安全
- Iterator,ListIterator,foreach ,Enumeration
在以后的开发之中,如果使用了List子接口,就使用ArrayList子类
总结:
1.List中的数据保存顺序就是数据添加顺序
2.List集合中可以保存有重复的元素
3.List子接口比Collection接口扩充了一个get()方法
4.List选择子类就是用ArrayList