20165328 课下作业补做

一、相关知识点总结:

  • 泛型的主要目的是可以建立具有类型安全的集合框架,如链表、散列映射等数据结构
  • 可以使用“class名称,<泛型列表>”声明一个类,为了和普通的类有所区别,这样声明的类称作泛型类,如:
class People<E>
  • java.util包中的LinkedList<E>泛型类创建的对象以链表结构存储数据,习惯上称LinkedList类创建的对象为链表对象,如:
LinkedList<String> mylist=new LinkedList<String>()

二、课上作业补交截图:

  • 排序:
  • 单链表:

三、第十五章代码分析:

  • Example15_1:
  • class Cone<E> { 
       double height;
       E bottom;           //用泛型类E声明对象bottom
       public Cone (E b) {
          bottom=b;   //给泛型类对象赋值
       }
       public void setHeight(double h) {
          height=h;
       }
       public double computerVolume() {
          String s=bottom.toString();//泛型变量只能调用从Object类继承的或重写的方法
          double area=Double.parseDouble(s); 
          return 1.0/3.0*area*height; 
       }
    }
    
    class Rect {
       double sideA,sideB,area; 
       Rect(double a,double b) {
         sideA=a;
         sideB=b;
       } 
       public String toString() {
          area=sideA*sideB;
          return ""+area;
       }
    }
    
    class Circle {
       double area,radius; 
       Circle(double r) {
          radius=r;
       } 
       public String toString() { //重写Object类的toString()方法
          area=radius*radius*Math.PI;
          return ""+area;
       }
    }
    
    public class Example15_1 {
       public static void main(String args[]) {
          Circle circle=new Circle(10);
          Cone<Circle> coneOne=new Cone<Circle>(circle);//创建一个(圆)锥对象  
          coneOne.setHeight(16);
          System.out.println(coneOne.computerVolume());
          Rect rect=new Rect(15,23);
          Cone<Rect> coneTwo=new Cone<Rect>(rect);//创建一个(方)锥对象
          coneTwo.setHeight(98); 
          System.out.println(coneTwo.computerVolume());
      }
    }

    该例子中,声明了一个泛类型Cone,一个Cone对象计算对象时,只关于它的底是否能计算面积,并不关心底的类型

  • Exampe15_2:
  • import java.util.*;
    public class Example15_2 {
       public static void main(String args[]){
          List<String> list=new LinkedList<String>();
          //定义一个空链表
          for(int i=0;i<=60096;i++){
                 list.add("speed"+i);
          }
          Iterator<String> iter=list.iterator();  //获取链表中的迭代器
          long starttime=System.currentTimeMillis();
          while(iter.hasNext()){
               String te=iter.next();
          }
          //进行遍历
          long endTime=System.currentTimeMillis();
          long result=endTime-starttime;
          System.out.println("使用迭代器遍历集合所用时间:"+result+"毫秒");
          starttime=System.currentTimeMillis();
          for(int i=0;i<list.size();i++){
              String te=list.get(i);
          }
          endTime=System.currentTimeMillis();
          result=endTime-starttime;
          System.out.println("使用get方法遍历集合所用时间:"+result+"毫秒");
        }
    }

    该例子比较了使用迭代器遍历链表和get(int index)方法遍历链表所用的时间

  • Example15_3:
  • import java.util.*;
    public class Example15_3 {
        public static void main(String args[]){
            LinkedList mylist=new LinkedList();//定义一个空链表
            mylist.add("");                 //链表中的第一个节点
            mylist.add("");                 //链表中的第二个节点
            int number=mylist.size();         //获取链表的长度
            for(int i=0;i<number;i++){
              String temp=(String)mylist.get(i); //必须强制转换取出的数据
              System.out.println(""+i+"节点中的数据:"+temp);
            } 
            Iterator iter=mylist.iterator();
            while(iter.hasNext()) {
              String te=(String)iter.next();  //必须强制转换取出的数据
              System.out.println(te);
            }
       }
    }

    该例子使用了JDK1.5版本之前的LinkedList

  • Example15_4:
  • import java.util.*;
    class Student implements Comparable { 
       int height=0;
       String name;
       Student(String n,int h) {
          name=n;
          height = h;
         
       }
       public int compareTo(Object b) { // 两个Student对象相等当且仅当二者的height值相等
         Student st=(Student)b;
         return (this.height-st.height);
       }
    }
    public class Example15_4 {
        public static void main(String args[ ]) { 
           List<Student> list = new LinkedList<Student>();
           //定义一个空链表
           list.add(new Student("张三",188));
           list.add(new Student("李四",178));
           list.add(new Student("周五",198)); 
           //向链表中依次添加元素
           Iterator<Student> iter=list.iterator();
           System.out.println("排序前,链表中的数据");
           while(iter.hasNext()){
              Student stu=iter.next();
              System.out.println(stu.name+ "身高:"+stu.height);
           }
           //对链表进行遍历
           Collections.sort(list);
           //进行升序排序
           System.out.println("排序后,链表中的数据");
           iter=list.iterator();
           while(iter.hasNext()){
              Student stu=iter.next();
              System.out.println(stu.name+ "身高:"+stu.height);
           }
           Student zhaoLin = new Student("zhao xiao lin",178);
           int index = Collections.binarySearch(list,zhaoLin,null);
           if(index>=0) {
                System.out.println(zhaoLin.name+"和链表中"+list.get(index).name+"身高相同");
           }
        }
    }

    该例子中,Student类通过实现Comparable接口规定该类的对象的大小关系

  • Example15_5:
  • import java.util.*;
    public class Example15_5 {
        public static void main(String args[ ]) { 
           List<Integer> list = new LinkedList<Integer>();
           //定义一个空链表
           for(int i=10;i<=50;i=i+10)
               list.add(new Integer(i));
           System.out.println("洗牌前,链表中的数据");
           Iterator<Integer> iter=list.iterator();
           while(iter.hasNext()){
              Integer n=iter.next();
              System.out.printf("%d\t",n.intValue());
           }
           //遍历链表
           Collections.shuffle(list);
           //按洗牌算法重新随机排列
           System.out.printf("\n洗牌后,链表中的数据\n");
           iter=list.iterator();
           while(iter.hasNext()){
              Integer n=iter.next();
              System.out.printf("%d\t",n.intValue());
           }
           System.out.printf("\n再向右旋转1次后,链表中的数据\n");
           Collections.rotate(list,1);
           //旋转链表中的数据
           iter=list.iterator();
           while(iter.hasNext()){
              Integer n=iter.next();
              System.out.printf("%d\t",n.intValue());
           }
          
        }
    }

    该例子使用了shuffle()方法、reverse()方法和rotate()

  • Example15_6:
  • import java.util.*;
    public class Example15_6 {
       public static void main(String args[]) {
          Stack<Integer> stack=new Stack<Integer>();
          //定义一个堆栈对象
          stack.push(new Integer(1)); 
          stack.push(new Integer(1));
          //进行压栈
          int k=1;
          while(k<=10) {
            for(int i=1;i<=2;i++) {
              Integer F1=stack.pop();
              int f1=F1.intValue();
              Integer F2=stack.pop();
              int f2=F2.intValue();
              Integer temp=new Integer(f1+f2);
              System.out.println(""+temp.toString()); 
              stack.push(temp);
              stack.push(F2);
              k++;
            }
          } 
       }
    }

    该例子用堆栈输出该递归序列的若干项

  • Example15_8:
  • import java.util.*;
    class Student implements Comparable {
       int english=0;
       String name;
       Student(int english,String name) {
          this.name=name;
          this.english=english;
       }
       public int compareTo(Object b) {
          Student st=(Student)b;
          return (this.english-st.english);
       }
    }
    public class Example15_8 {
      public static void main(String args[]) {
         TreeSet<Student> mytree=new TreeSet<Student>();
         //定义一个树集对象
         Student st1,st2,st3,st4;
         st1=new Student(90,"赵一");
         st2=new Student(66,"钱二");
         st3=new Student(86,"孙三");
         st4=new Student(76,"李四");
         mytree.add(st1);
         mytree.add(st2);
         mytree.add(st3);
         mytree.add(st4);
         //依次添加数据
         Iterator<Student> te=mytree.iterator();
         while(te.hasNext()) {
            Student stu=te.next();
            System.out.println(""+stu.name+" "+stu.english);
            //遍历并打印数据
         }
      }
    }

    该例子中的树集按着英语成绩从高到低存放4个Student对象

posted @ 2018-05-06 22:33  20165328  阅读(244)  评论(0编辑  收藏  举报