集合框架中的接口 第六课A-1 11000
Collection 集合层次中的根接口,jdk中没有提供这个接口的直接实现类
Set:不能包含重复的元素。SortedSet是个按照升序排列元素的Set,她完全由Collection接口继承而来。
List:是一个有序集合,可以包含重复的元素。这里的有序是指在List队列中元素是按照一定次序排列的,元素的位置很重要,不能随意更改。提供了按索引方式访问
而在Collection Set接口中就只有add()方法,而没有相应get()方法。
Map:key-value成对出现。不包含重复的key。SortedMap是按照升序排列的Map
集合框架中的实现类 第六课a-2 0000
ArrayList:
·我们可以看着是能够自动增长容量的数组。ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。
·利用ArrayList中的toArray()方法可以返回一个数组(后边有例子) toArray方法返回的是一个object对象数组
·Arrays.asList()可以返回一个列表,她可以将一个对象数组转化返回一个List对象列表。但是注意,她返回的列表在这里是个固定大小的列表,所以可以修改,但是不能增加元素。
其实Arrays.asList()方法和ArrayList.toArray()方法实现了数组与集合列表之间的转换功能。数组可以转化成列表,列表也可以转化成数组。(后边有例子)但是注意,通过Arrays.asList()生成的列表长度是固定的,不能再增加元素了。
·迭代器(iterator)为我们提供了通用的方式来访问集合中的元素
一.使用ArrayList集合类优点
1。支持自动改变大小的功能
2。可以灵活的插入元素
3。可以灵活的删除元素
二.使用ArrayList集合类局限性
跟一般的数组比起来,速度上差些
下面练习如何向ArrayList集合类添加元素,并通过集合方式打印和转化成数组方式打印:
//此文件定义一个ArrayList集合类,
//注意列表和数组都是如果访问个中元素的,并通过集合方式打印和转化成数组方式打印
import java.util.*; //因为使用了ArrayList类
public class ArrayListTest
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Point(2,3));
al.add(new Point(4,5));
al.add(new Point(7,8));
//用集合的方法打印al各个元素
System.out.println("用集合的方法打印al各个元素");
for(int i=0;i<al.size();i++)
{
System.out.println(al.get(i));
}
//也可以通过直接调用对象的toString()方法打印咯,此例是覆盖了toString()方法
System.out.println("通过直接调用对象的toString()方法打印");
System.out.println(al);
//可以将集合列表转化成数组再打印
Object[] obt =al.toArray(); //因为toArray方法返回的是一个object对象数组,所以这样定义
System.out.println("将集合列表转化成数组再打印");
for(int i=0;i<obt.length;i++)
{
System.out.println(obt[i]);
}
//将数组转化成集合列表
System.out.println("将数组转化成集合列表");
List lt = Arrays.asList(obt);
System.out.println(lt);
}
}
下面讲一下迭代器(Iterator接口)第六课 b 4700
Iterator是java.util中的一个接口。她有3个方法:
·hasNext() 判断是否有更多元素。返回true或flase
·next() 用此方法之前一般先用hasNext()方法判断是否有更多元素,如果有就返回下一个元素。返回类型是Object。
·remove() 删除我们所返回的上一个元素。所以如果要删除某个元素,必须至少调用一次next()方法。这是一个可选方法,在Iterator接口中并没有实现这个方法。因为一般使用迭代器都是访问读取元素内容,而不会删除元素。但是有时候列表也需要删除操作,所以把她设为可选的方法。如果你不想使用此方法,在使用Iterator对象时会抛出一个UnSupportedOperationException异常,你只需要捕获或者抛出这个异常,如果你要使用这个方法,你就实现这个方法。
幸运的是在我们常用的集合框架中都实现了remove方法,所有实现了的常用集合类都可以随便使用。但是通过Arrays.asList()返回的List对象并没有真正实现该方法,所以对此List对象使用remove方法就会抛出UnSupportedOperationException异常.后面有例子。
//用迭代器访问各个元素:
import java.util.*; //因为使用了ArrayList类
public class ArrayListTest
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Point(2,3));
al.add(new Point(4,5));
al.add(new Point(7,8));
//用迭代器访问各个元素
System.out.println("用迭代器访问各个元素");
Iterator it = al.iterator();//因为在Collection接口中就有iterator()这个方法来返回一个迭代器对象,
//所以只要是从Collection接口实现或继承而来的对象类型,就可以用她的iterator()方法来返回一个迭代器
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
如果要使用迭代器删除列表中的元素怎么做呢??像下面这样???
import java.util.*; //因为使用了ArrayList类
public class ArrayListTest
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Point(2,3));
al.add(new Point(4,⨪⨪⨪⨪
⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪
⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪
⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪
⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪
⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪
⨪⨪⨪⨪
⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪
⨪⨪⨪⨪
⨪⨪
⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪⨪ove()就能把刚才返回的元素删除了。向下面这个就吧Point(2,3)删除了:
import java.util.*; //因为使用了ArrayList类
public class ArrayListTest
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Point(2,3));
al.add(new Point(4,5));
al.add(new Point(7,8));
System.out.println("用迭代器访问各个元素");
Iterator it = al.iterator();
it.next();
it.remove();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
所有实现了的常用集合类都可以随便使用remove方法。但是通过Arrays.asList()返回的List对象并没有真正实现该方法,所以对此List对象使用remove方法就会抛出UnSupportedOperationException异常,例如:
import java.util.*; //因为使用了ArrayList类
public class ArrayListTest
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add(new Point(2,3));
al.add(new Point(4,5));
al.add(new Point(7,8));
Object obj =al.toArray();
List ll = Arrays.asList(obj);
Iterator it = ll.iterator();
it.next();
it.remove();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
迭代器作用:
·有些集合类没有实现size()和get(i)这种方式访问元素,那么这时就可以选择迭代器方式访问
·迭代器给我们提供了通用的访问集合列表的方法。
Collections类介绍: 第六课 c
Collections主要是用来对列表进行操作,而Arrays类主要用来对数组进行操作。这就是他们的区别。下面讲Collections类的用法:
·排序:Collections.sort() 主要是对List列表排序
1 自然排序
sort()方法:遵照自然排序进行升序排列。该方法参数中所有的list对象都要求实现了comparable接口。要实现这个接口就要实现其中的compareTo()方法。返回正数零或者负数。
例子:
import java.util.*; //因为使用了ArrayList类
public class ArrayListTest
{
//定义一个通用的迭代器打印方法
public static void printElements(Collection c)
{
Iterator it =c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
//创建Student对象
Student sd1 =new Student(3,"wangwu");
Student sd2 =new Student(1,"lisi");
Student sd3 =new Student(2,"zhangshan");
Student sd4 =new Student(4,"yeba");
//定义一个集和列表用于存储Student对象
ArrayList al =new ArrayList();
//向列表中添加元素
al.add(sd1);
al.add(sd2);
al.add(sd3);
al.add(sd4);
//通过Collections.sort()方法对列表al对象进行自然排序
Collections.sort(al);
//打印列表中的元素
printElements(al);
}
}
class Student implements Comparable
{
int num;
String name;
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public int compareTo(Object o)
{
Student st =(Student)o;
return num>st.num?1:(num==st.num?0:-1);
}
public String toString()
{
return "num="+num+"; name="+name;
}
}
2 实现比较器(comparator)接口
通过比较器呢可以在对某个列表进行排序的时候传递一个比较器对象,让她按照我们事先定义好的排序规则进行排序。
比较器接口中有一个方法compare(Object o1,Object o2)必须要实现。还是返回int类型,对象1>对象2时返回正数,对象1<对象2时返回负数,对象1=对象2时返回0.
import java.util.*; //因为使用了ArrayList类
public class ArrayListTest
{
//定义一个通用的迭代器打印方法
public static void printElements(Collection c)
{
Iterator it =c.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args)
{
//创建Student对象
Student sd1 =new Student(3,"wangwu");
Student sd2 =new Student(1,"lisi");
Student sd3 =new Student(2,"zhangshan");
Student sd4 =new Student(2,"shanshan");
Student sd5 =new Student(4,"yeba");
//定义一个集和列表用于存储Student对象
ArrayList al =new ArrayList();
//向列表中添加元素
al.add(sd1);
al.add(sd2);
al.add(sd3);
al.add(sd4);
al.add(sd5);
//通过Collections.sort()方法对列表al对象进行自然排序
Collections.sort(al);
System.out.println("通过Collections.sort()方法对列表al对象进行自然排序");
printElements(al); //打印列表中的元素
//通过Collections.sort()方法排序时还可以传递一个比较器参数:
Collections.sort(al,new Student.StudentComparator());
System.out.println("通过Collections.sort()方法排序时还可以传递一个比较器参数");
printElements(al); //打印传递一个比较器参数之后列表中的元素
}
}
class Student implements Comparable
{
int num;
String name;
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public int compareTo(Object o)
{
Student st =(Student)o;
return num>st.num?1:(num==st.num?0:-1);
}
public String toString()
{
return "num="+num+"; name="+name;
}
//我们还可以给Student类定义一个比较器,这里通过内部类实现
public static class StudentComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Student st1 =(Student)o1;
Student st2 =(Student)o2;
int result;
result=st1.num>st2.num?1:(st1.num==st2.num?0:-1);
if(result==0)//如果学号相等 就进行名字排序
{
result=st1.name.compareTo(st2.name);
}
return result;
}
}
}
在Collections类中还有个public static void reverse(List<?> list)方法,就是反向排序。所以入股你要降序排序就可以向下面这样:(注意,该方法返回就是一个比较器对象):
Collections.sort(al,Collections.reverseOrder());
·取最大和最小的元素:Collections.max() Collections.min()
·在已排序的List中搜索指定元素:Collections.binary.Search()