国产最新a级毛片无码专区_综合亚洲欧美日韩久久精品_日本成年片在线观看66_一本到九九av电影_一级毛片免费网站播放_国内精品久久人无码大片_国产人成视频99在线观看_欧美不卡在线一本二本_国产亚洲电影av_可以免费看黄色软件

知ing

Java2實(shí)用教程(第4版)

耿祥義、張躍平 / 清華大學(xué)出版社

頑張って! 上傳

查看本書(shū)

習(xí)題十五(第15章)

一、問(wèn)答題

1LinkedList使用鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu),ArrayList使用順序存儲(chǔ)結(jié)構(gòu)。

2迭代器遍歷在找到集合中的一個(gè)對(duì)象的同時(shí),也得到待遍歷的后繼對(duì)象的引用,因此迭代器可以快速地遍歷集合。

3不是。

4HashMap<K,V>來(lái)存儲(chǔ)。

二、閱讀程序

18。

2ABCD

三、編寫(xiě)程序

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+"");

??????}

????}

}

習(xí)題十六(第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("輸入一個(gè)數(shù)回車(chē)確定:"));

??????box1.add(text1);

??????label=new Label("數(shù)的平方:");

??????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>*/

?


查看更多