15-1泛型概念
泛型的主要目的是为了建立具有类型安全的集合框架,如链表,散列映射等数据结构。//什么意思我也不懂
创建的语法是:clss People<E>
People是这个泛型的名字,E就是其中的泛型
例:底面是圆的锥和底面是长方形的锥,它们的求体积的方法都是一样的,都是底面积乘高再除以3,这里把锥泛化成一个类型class Cone
package Example15_1;
public class Cone<E> {
double height;
E bottom;
public Cone(E b){
bottom=b;
}
public void setHeight(double b){
height=b;
}
public double Volume(){
String s=bottom.toString();
double area=Double.parseDouble(s);
return 1.0/3.0*area*height;
}
}
下面分别是长方形和圆的函数
package Example15_1;
public class Rect {
double sideA,sideB,area;
Rect(double a,double b){ //构造长方形
sideA=a;
sideB=b;
}
public String toString(){ //重写Object类的toString()方法,让它返回长方形的面积
area=sideA*sideB;
return ""+area;
}
}
package Example15_1;
public class Circle {
double area,radius;
public Circle(double r) {
// TODO Auto-generated constructor stub
radius=r;
}
public String toString(){
area=radius*radius*Math.PI;
return ""+area; //因为Object类的toString()方法返回的String类型,所以这里的area前加个空串
}
}
主函数:
package Example15_1;
public class Example15_1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle circle=new Circle(10);
Cone<Circle>ConeOne=new Cone<Circle>(circle);//泛型出来bottom是Circle类型的一个对象
ConeOne.setHeight(54);//设置底面为圆的锥的高
ConeOne.Volume(); //调用泛型里的求体积方法
System.out.println("底面为圆的锥形的体积是"+ConeOne.Volume());
Rect rect=new Rect(58, 598);//new一个Rect的对象
Cone<Rect>ConeTwo=new Cone<Rect>(rect);
ConeTwo.setHeight(4);
ConeTwo.Volume();
System.out.println("底面是长方形的锥的体积是"+ConeTwo.Volume());
}
}
15-2链表的迭代器与get方法
链表是几个元素具有相互连接的元素的表,A→B→C ,B有A的地址,B有C的地址,这样好查找。
java.util包中有个类LinkList,使用它创建的对象为链表对象。
LinkList<E>list=new LinkList<E>();
往list表中添加数据的方法:list.add();
15-2.2链表的遍历
LinkList表不是顺序结构,因此调用get()方法会比较慢,不过链表提供了迭代器,链表对象可以调用iterator()方法返回一个Iterator对象,该对象就是这个链表的迭代器。下面比较一下两种方法的遍历时间:
package Examle15_2;
import java.util.Iterator;
import java.util.LinkedList;
public class Examle15_2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList<String>list=new LinkedList<String>(); //LinkedList类是java.util包中的一个泛型类
for(int i=0;i<6000;i++){//往LinkedList链表中添加600个节点
list.add("speed"+i);
}
Iterator<String>iter=list.iterator();//list使用iterator()方法获得的iter对象是当前链表的迭代器
long startTime1=System.currentTimeMillis();//执行查询的开始时间
while(iter.hasNext()){
String e=iter.next(); //把从链表查询的每个结点都给e
}
long endTime1=System.currentTimeMillis();
System.out.println("使用迭代器查询使用的时间是:"+(endTime1-startTime1));
long startTime2=System.currentTimeMillis();
for(int i=0;i<list.size();i++){
String e=list.get(i);//list的get方法,得到链表的第i个元素
}
long endTime2=System.currentTimeMillis();
System.out.println("使用get方法查询使用的时间是:"+(endTime2-startTime2));
}
}
15-3压栈与弹栈
栈是一种”后入先出“的数据结构,利用栈可以节省内存
声明栈的方法是Stack<E>stack=new Stack<E>();
然后可以使用stack变量调用方法pop()
弹栈push()
压栈数据
package Example15_3;
import java.util.Stack;
public class Example15_3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<Integer>stack=new Stack<Integer>();
stack.push(new Integer(1));//先压栈Integer类型的值为1
stack.push(new Integer(1));//先压栈Integer类型的值为1
int k=1;
while(k<=10){
for(int i=1;i<=2;i++){
Integer F1=stack.pop(); //弹栈,Integer类型的F1
int f1=F1.intValue(); //F1的int型值
Integer F2=stack.pop();
int f2=F2.intValue();
Integer temp= new Integer(f1+f2); //创建Integer类型的temp,是F1和F2的和
System.out.println(temp.toString());
stack.push(temp);//压栈,先temp再F2,顺序不能错
stack.push(F2);
k++;
}
}
}
}
15-4树集——TreeSet泛型类
TreeSet类是实现Set接口的类,它的大部分方法都是接口方法。TreeSet类创建的对象称为树集,结点中的数据会按照”同层自左向右依次增大,自上向下依次减小“的规则排列。而增加结点的方法是add().
例:
import java.util.Iterator;
import java.util.TreeSet;
class Student implements Comparable<Object>{
int english=0;
String name;
Student(int english,String name){ //构造方法
this.english=english;
this.name=name;
} public int compareTo(Object arg0) {//比较英语成绩,如果相同返回1,不同就默认
// TODO Auto-generated method stub
Student st=(Student)arg0;
if((this.english-st.english)==0)return 1;
else return (this.english-st.english);
}
}
public class Example15_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<Student>mytree=new TreeSet<Student>(); //学生的二叉树泛型
Student st1=new Student(45, "张一");
Student st2=new Student(486, "李二");
Student st3=new Student(455, "赵三");
Student st4=new Student(485, "王四");
mytree.add(st1);
mytree.add(st2);
mytree.add(st3);
mytree.add(st4);
Iterator<Student>iterator=mytree.iterator();
while(iterator.hasNext()){
Student stu=iterator.next(); //遍历的结果给这个学生变量
System.out.println("学生的姓名:"+stu.name+",学生的成绩:"+stu.english);
}
}
}
15-5树映射
TreeMap<K,V>类实现了Map<K,V>的接口,称TreeMap<K,V>对象为树映射。树映射使用public V put(K key,V value)方法添加结点,该结点不仅存储数据value,也存储关键字key。树映射使用结点中的关键字排序。
package Example15_4;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
class StudentKey implements Comparable{
double d=0;
StudentKey(double d){
this.d=d;
}
@Override
public int compareTo(Object arg0) { //比较方法的定义
// TODO Auto-generated method stub
StudentKey st=(StudentKey)arg0;
if((this.d-st.d)==0){
return -1;
}
else return (int)((this.d-st.d)*1000);
}
}
class Student{ //只是Student类,与Compare()解耦合
String name=null;
double english,math;
Student(String n,double e,double m){
this.name=n;
this.english=e;
this.math=m;
}
}
public class Example15_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeMap<StudentKey, Student>treeMap=new TreeMap<StudentKey, Student>();//TreeMap<K,V>的使用
String st[]={"张三","李四","赵武","王二"};
double eng[]={56,58,65,886};
double mat[]={56,58,65,886};
Student stu[]=new Student[4];
for(int k=0;k<stu.length;k++){
stu[k]=new Student(st[k], eng[k], mat[k]);
}
StudentKey key[]=new StudentKey[4];
for(int i=0;i<4;i++){//以数学成绩为排序的依据
key[i]=new StudentKey(stu[i].math);
}
for(int i=0;i<key.length;i++){ //放进去
treeMap.put(key[i],stu[i]);
}
int number=treeMap.size();//返回树的大小
System.out.println("树的大小:"+number);
Collection<Student>collection=treeMap.values();//跟链表比要复杂一点,先让树对象返回这个树的集合,给collection对象
Iterator<Student>iterator=collection.iterator();//然后才是迭代器的对象这步
System.out.println(" 使用数学排序的结果是:");
while(iterator.hasNext()){
Student stuMath=iterator.next();//强制转换一下,不然输出的是iterator的地址
System.out.println("姓名:"+stuMath.name+" 数学:"+stuMath.math);
}
treeMap.clear();//把数据清空,下面放入英语成绩
for(int i=0;i<4;i++){//以数学成绩为排序的依据
key[i]=new StudentKey(stu[i].english);
}
for(int i=0;i<key.length;i++){ //放进去
treeMap.put(key[i],stu[i]);
}
collection=treeMap.values();
iterator=collection.iterator();
System.out.println("使用英语排序的结果是:");
while(iterator.hasNext()){
Student stuEng=iterator.next();//强制转换一下,不然输出的是iterator的地址
System.out.println("姓名:"+stuEng.name+" 数学:"+stuEng.math);
}
}
}