一、問(wèn)答題
1.LinkedList使用鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu),ArrayList使用順序存儲(chǔ)結(jié)構(gòu)。
2.迭代器遍歷在找到集合中的一個(gè)對(duì)象的同時(shí),也得到待遍歷的后繼對(duì)象的引用,因此迭代器可以快速地遍歷集合。
3.不是。
4.用HashMap<K,V>來(lái)存儲(chǔ)。
二、閱讀程序
1.8。
2.ABCD。
三、編寫(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+"元");
??????}
????}
}
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>*/
?