知ing

Java2实用教程(第4版)

耿祥义、张跃平 / 清华大学出版社

頑張って! 上传

查看本书

习题十五(第15章)

一、问答题

1LinkedList使用链式存储结构,ArrayList使用顺序存储结构。

2迭代器遍历在找到集合中的一个对象的同时,也得到待遍历的后继对象的引用,因此迭代器可以快速地遍历集合。

3不是。

4HashMap<K,V>来存储

二、阅读程序

18

2ABCD

三、编写程序

1.  import java.util.*;

public class E {

   public static void main(String args[]) {

      Stack<Integer> stack=new Stack<Integer>();

      stack.push(new Integer(3));

      stack.push(new Integer(8));

      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(2*f1+2*f2);

          System.out.println(""+temp.toString());

          stack.push(temp);

          stack.push(F2);

          k++;

        }

      }

   }

}

2.   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 E {

  public static void main(String args[]) {

     List<Student> list=new LinkedList<Student>();

     int score []={65,76,45,99,77,88,100,79};

     String name[]={"张三","李四","旺季","加戈","为哈","周和","赵李","将集"};

     for(int i=0;i<score.length;i++){

             list.add(new Student(score[i],name[i]));

     }

     Iterator<Student> iter=list.iterator();

     TreeSet<Student> mytree=new TreeSet<Student>();

     while(iter.hasNext()){

         Student stu=iter.next();

         mytree.add(stu);

     }

     Iterator<Student> te=mytree.iterator();

     while(te.hasNext()) {

        Student stu=te.next();

        System.out.println(""+stu.name+" "+stu.english);

     }

  }

}

3.  import java.util.*;

class UDiscKey implements Comparable {

   double key=0;

   UDiscKey(double d) {

     key=d;

   }

   public int compareTo(Object b) {

     UDiscKey disc=(UDiscKey)b;

     if((this.key-disc.key)==0)

        return -1;

     else

        return (int)((this.key-disc.key)*1000);

  }

}

class UDisc{

    int amount;

    double price;

    UDisc(int m,double e) {

       amount=m;

       price=e;

   }

}

public class E {

   public static void main(String args[ ]) {

      TreeMap<UDiscKey,UDisc>  treemap= new TreeMap<UDiscKey,UDisc>();

      int amount[]={1,2,4,8,16};

      double price[]={867,266,390,556};

      UDisc UDisc[]=new UDisc[4];

      for(int k=0;k<UDisc.length;k++) {

         UDisc[k]=new UDisc(amount[k],price[k]);

      }

      UDiscKey key[]=new UDiscKey[4] ;

      for(int k=0;k<key.length;k++) {

         key[k]=new UDiscKey(UDisc[k].amount);

      }

      for(int k=0;k<UDisc.length;k++) {

         treemap.put(key[k],UDisc[k]);          

      }

      int number=treemap.size();

      Collection<UDisc> collection=treemap.values();

      Iterator<UDisc> iter=collection.iterator();

      while(iter.hasNext()) {

         UDisc disc=iter.next();

         System.out.println(""+disc.amount+"G "+disc.price+"");

      }

      treemap.clear();

      for(int k=0;k<key.length;k++) {

         key[k]=new UDiscKey(UDisc[k].price);

      }

      for(int k=0;k<UDisc.length;k++) {

         treemap.put(key[k],UDisc[k]);

      }

      number=treemap.size();

      collection=treemap.values();

      iter=collection.iterator();

      while(iter.hasNext()) {

         UDisc disc=iter.next();

         System.out.println(""+disc.amount+"G "+disc.price+"");

      }

    }

}

习题十六(第16章)

1 import java.applet.*;

import java.awt.*;

import java.awt.event.*;  

import javax.swing.*;

public class Xiti2 extends Applet implements ActionListener

{  TextField text1,text2;

   Label label;

   public void init()

   {  text1=new TextField(10);

      text2=new TextField(20);

      Box box1=Box.createHorizontalBox();

      Box box2=Box.createHorizontalBox();

      Box boxV=Box.createVerticalBox();

      box1.add(new Label("输入一个数回车确定:"));

      box1.add(text1);

      label=new Label("数的平方:");

      box2.add(label);

      box2.add(text2);

      boxV.add(box1);

      boxV.add(box2);

      add(boxV);

      text2.setEditable(false);

      text1.addActionListener(this);

   }

   public void actionPerformed(ActionEvent e)

   {  String number=e.getActionCommand();

      try{ double n=Double.parseDouble(number);

           double m=n*n;

           label.setText(n+"的平方:");

           text2.setText(""+m);

           text1.setText("");

           validate();

         }

      catch(NumberFormatException exp)

         {  text2.setText(""+exp);

         }

   }

}

2 import java.applet.*;

import java.awt.*;

public class Rect extends Applet {

   int w,h;

   public void init() {

      String s1=getParameter("width");  //html得到"width"的值。

      String s2=getParameter("height");  //html得到"height"的值。

      w=Integer.parseInt(s1);

      h=Integer.parseInt(s2);

   }

   public void paint(Graphics g) {

      g.drawRect(10,10,w,h);

   }

}

/*<applet code= Rect.class width=280 height=500>

     <Param name="width"  value ="150">

     <Param name="height"  value ="175">

</applet>*/

 


查看更多