Java2实用教程(第二版)程序代码——第二十六章 常见数据结构的Java实现
1
//例子1
2
import java.util.*;
3
public class LinkListOne
4
{public static void main(String args[])
5
{ LinkedList mylist=new LinkedList();
6
mylist.add("It"); //链表中的第一个节点。
7
mylist.add("is"); //链表中的第二个节点。
8
mylist.add("a"); //链表中的第三个节点。
9
mylist.add("door"); //链表中的第四个节点。
10
int number=mylist.size(); //获取链表的长度。
11
for(int i=0;i<number;i++)
12
{String temp=(String)mylist.get(i);
13
System.out.println("第"+i+"节点中的数据:"+temp);
14
}
15
}
16
}
17![]()
18
//例子2
19
import java.util.*;
20
public class LinkListTwo
21
{public static void main(String args[])
22
{ LinkedList mylist=new LinkedList();
23
mylist.add("is"); mylist.add("a");
24
int number=mylist.size();
25
System.out.println("现在链表中有"+number+"个节点:");
26
for(int i=0;i<number;i++)
27
{String temp=(String)mylist.get(i);
28
System.out.println("第"+i+"节点中的数据:"+temp);
29
}
30
mylist.addFirst("It");mylist.addLast("door");
31
number=mylist.size();
32
System.out.println("现在链表中有"+number+"个节点:");
33
for(int i=0;i<number;i++)
34
{String temp=(String)mylist.get(i);
35
System.out.println("第"+i+"节点中的数据:"+temp);
36
}
37
mylist.remove(0);mylist.remove(1);
38
mylist.set(0,"open");
39
number=mylist.size();
40
System.out.println("现在链表中有"+number+"个节点:");
41
for(int i=0;i<number;i++)
42
{String temp=(String)mylist.get(i);
43
System.out.println("第"+i+"节点中的数据:"+temp);
44
}
45
}
46
}
47![]()
48
//例子3
49
import java.util.*;
50
class Student
51
{String name ;int number;float score;
52
Student(String name,int number,float score)
53
{this.name=name;this.number=number;this.score=score;
54
}
55
}
56
public class LinkListThree
57
{public static void main(String args[])
58
{ LinkedList mylist=new LinkedList();
59
Student stu_1=new Student("赵好民" ,9012,80.0f),
60
stu_2=new Student("钱小青" ,9013,90.0f),
61
stu_3=new Student("孙力枚" ,9014,78.0f),
62
stu_4=new Student("周左右" ,9015,55.0f);
63
mylist.add(stu_1); mylist.add(stu_2);
64
mylist.add(stu_3); mylist.add(stu_4);
65
Iterator iter=mylist.iterator();
66
while(iter.hasNext())
67
{ Student te=(Student)iter.next();
68
System.out.println(te.name+" "+te.number+" "+te.score);
69
}
70
}
71
}
72![]()
73
//例子4
74
import java.util.*;import java.awt.event.*;import java.awt.*;
75
import javax.swing.*;import java.io.*;
76
class 商品 extends Panel
77
{String 代号,名称;int 库存;float 单价;
78
商品(String 代号,String 名称,int 库存,float 单价)
79
{this.代号=代号;this.名称=名称;this.库存=库存;this.单价=单价;
80
}
81
}
82![]()
83
class ShowWin extends JFrame implements ActionListener
84
{ LinkedList goods_list=null;
85
JTextField 代号文本框=new JTextField(),
86
名称文本框=new JTextField(),
87
库存文本框=new JTextField(),
88
单价文本框=new JTextField(),
89
删除文本框=new JTextField();
90
JButton b_add=new JButton("添加商品"),
91
b_del=new JButton("删除商品"),
92
b_show =new JButton("显示商品清单");
93
JTextArea 显示区=new JTextArea();
94
ShowWin()
95
{goods_list=new LinkedList();
96
Container con=getContentPane();
97
JScrollPane pane=new JScrollPane(显示区);
98
显示区.setEditable(false);
99
JPanel save=new JPanel();save.setLayout(new GridLayout(5,2));
100
save.add(new Label("输入代号:"));save.add(代号文本框);
101
save.add(new Label("输入名称:"));save.add(名称文本框);
102
save.add(new Label("输入库存:"));save.add(库存文本框);
103
save.add(new Label("输入单价:"));save.add(单价文本框);
104
save.add(new Label("点击添加:"));save.add(b_add);
105
JPanel del=new JPanel();del.setLayout(new GridLayout(2,2));
106
del.add(new Label("输入删除的代号:"));del.add(删除文本框);
107
del.add(new Label("点击删除:"));del.add(b_del);
108
JPanel show=new JPanel();show.setLayout(new BorderLayout());
109
show.add(pane,BorderLayout.CENTER);show.add(b_show,BorderLayout.SOUTH);
110
JSplitPane split_one,split_two;
111
split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,save,del);
112
split_two=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,split_one,show);
113
con.add(split_two,BorderLayout.CENTER);
114
b_add.addActionListener(this);b_del.addActionListener(this);
115
b_show.addActionListener(this);
116
}
117
public void actionPerformed(ActionEvent e)
118
{if(e.getSource()==b_add)
119
{String daihao=null,mingcheng=null;int kucun=0;float danjia=0.0f;
120
daihao=代号文本框.getText();mingcheng=名称文本框.getText();
121
kucun=Integer.parseInt(库存文本框.getText());
122
danjia=Float.valueOf(单价文本框.getText()).floatValue();
123
商品 goods=new 商品(daihao,mingcheng,kucun,danjia);
124
goods_list.add(goods);
125
try {FileOutputStream file=new FileOutputStream("goods.txt");
126
ObjectOutputStream out=new ObjectOutputStream(file);
127
out.writeObject(goods_list);out.close();
128
}
129
catch(IOException event){}
130
}
131
else if(e.getSource()==b_del)
132
{String daihao=删除文本框.getText();
133
try {FileInputStream come_in=new FileInputStream("goods.txt");
134
ObjectInputStream in=new ObjectInputStream(come_in);
135
goods_list=(LinkedList)in.readObject();in.close();
136
}
137
catch(ClassNotFoundException event){}
138
catch(IOException event){}
139
for(int i=0;i<goods_list.size();i++)
140
{商品 temp=(商品)goods_list.get(i);
141
if(temp.代号.equals(daihao)) {goods_list.remove(i);}
142
try {FileOutputStream file=new FileOutputStream("goods.txt");
143
ObjectOutputStream out=new ObjectOutputStream(file);
144
out.writeObject(goods_list);
145
out.close();
146
}
147
catch(IOException event){}
148
}
149
}
150
else if(e.getSource()==b_show)
151
{ 显示区.setText(null);
152
try {FileInputStream come_in=new FileInputStream("goods.txt");
153
ObjectInputStream in=new ObjectInputStream(come_in);
154
goods_list=(LinkedList)in.readObject();
155
}
156
catch(ClassNotFoundException event){}
157
catch(IOException event){}
158
Iterator iter=goods_list.iterator();
159
while(iter.hasNext())
160
{ 商品 te=(商品)iter.next();
161
显示区.append("商品代号:"+te.代号+" ");
162
显示区.append("商品名称:"+te.名称+" ");
163
显示区.append("商品库存:"+te.库存+" ");
164
显示区.append("商品单价:"+te.单价+" ");
165
显示区.append("\n");
166
}
167
}
168
}
169
}
170
public class LinkListFour
171
{public static void main(String args[])
172
{ ShowWin win=new ShowWin();
173
win.setSize(100,100);
174
win.setVisible(true);
175
win.addWindowListener(new WindowAdapter()
176
{public void windowClosing(WindowEvent e)
177
{System.exit(0);}});
178
}
179
}
180![]()
181
//例子5
182
import java.util.*;
183
class StackOne
184
{public static void main(String args[])
185
{Stack mystack=new Stack();
186
mystack.push(new Integer(1)); mystack.push(new Integer(2));
187
mystack.push(new Integer(3)); mystack.push(new Integer(4));
188
mystack.push(new Integer(5)); mystack.push(new Integer(6));
189
while(!(mystack.empty()))
190
{Integer temp=(Integer)mystack.pop();
191
System.out.print(" "+temp.toString());}
192
}
193
}
194![]()
195
//例子6
196
import java.util.*;
197
class StackTwo
198
{public static void main(String args[])
199
{Stack mystack=new Stack();
200
mystack.push(new Integer(1)); mystack.push(new Integer(1));
201
int k=1;
202
while(k<=10)
203
for(int i=1;i<=2;i++)
204
{Integer F1=(Integer)mystack.pop();int f1=F1.intValue();
205
Integer F2=(Integer)mystack.pop();int f2=F2.intValue();
206
Integer temp=new Integer(f1+f2);
207
System.out.println(""+temp.toString());
208
mystack.push(temp);mystack.push(F2);k++;
209
}
210
}
211
}
212![]()
213
//例子7
214
import java.util.*;
215
class TreeOne
216
{public static void main(String args[])
217
{ TreeSet mytree=new TreeSet();
218
mytree.add("boy");mytree.add("zoo");
219
mytree.add("apple"); mytree.add("girl");
220
Iterator te=mytree.iterator();
221
while(te.hasNext())
222
System.out.println(""+te.next());
223
}
224
}
225![]()
226
//例子8
227
import java.util.*;import java.awt.*;
228
class TreeTwo
229
{public static void main(String args[])
230
{ TreeSet mytree=new TreeSet(new Comparator()
231
{public int compare(Object a,Object b)
232
{Student stu1=(Student)a;Student stu2=(Student)b;
233
return stu1.compareTo(stu2);}
234
});
235
Student st1,st2,st3,st4;
236
st1=new Student(90,"zhan ying");st2=new Student(66,"wang heng");
237
st3=new Student(86,"Liuh qing");st4=new Student(76,"yage ming");
238
mytree.add(st1);mytree.add(st2);mytree.add(st3);mytree.add(st4);
239
Iterator te=mytree.iterator();
240
while(te.hasNext())
241
{Student stu=(Student)te.next();
242
System.out.println(""+stu.name+" "+stu.english);
243
}
244
}
245
}
246
class Student implements Comparable
247
{ int english=0;String name;
248
Student(int e,String n)
249
{english=e;name=n;
250
}
251
public int compareTo(Object b)
252
{ Student st=(Student)b;
253
return (this.english-st.english);
254
}
255
}
256![]()
257
//例子9
258
import java.util.*;import java.awt.event.*;
259
import java.awt.*;
260
class 节目 implements Comparable
261
{String name;double time;
262
节目(String 名称,double 演出时间)
263
{name=名称;time=演出时间;
264
}
265
public int compareTo(Object b)
266
{节目 item=(节目)b;
267
return (int)((this.time-item.time)*1000);
268
}
269
}
270![]()
271
class Win extends Frame implements ActionListener
272
{ TreeSet 节目清单=null;
273
TextField 名称文本框=new TextField(10),
274
时间文本框=new TextField(5),
275
删除文本框=new TextField(5);
276
Button b_add=new Button("添加节目"),
277
b_del=new Button("删除节目"),
278
b_show =new Button("显示节目清单");
279
TextArea 显示区=new TextArea();
280
Win()
281
{ 节目清单=new TreeSet(new Comparator()
282
{public int compare(Object a,Object b)
283
{节目 item_1=(节目)a;
284
节目 item_2=(节目)b;
285
return item_1.compareTo(item_2);
286
}
287
});
288
Panel 节目单输入区=new Panel();
289
节目单输入区.add(new Label("节目名称:"));
290
节目单输入区.add(名称文本框);
291
节目单输入区.add(new Label("演出时间:"));
292
节目单输入区.add(时间文本框);
293
节目单输入区.add(new Label("点击添加:"));
294
节目单输入区.add(b_add);
295
节目单输入区.add(b_show);
296
Panel 节目单删除区=new Panel();
297
节目单删除区.add(new Label("输入演出的时间:"));
298
节目单删除区.add(删除文本框);
299
节目单删除区.add(new Label("点击删除:"));
300
节目单删除区.add(b_del);
301
Panel 节目单显示区=new Panel();
302
节目单显示区.add(显示区);
303
显示区.setBackground(Color.pink);
304
b_add.addActionListener(this);b_del.addActionListener(this);
305
b_show.addActionListener(this);
306
add(节目单输入区,"North");add(节目单显示区,"Center");
307
add(节目单删除区,"South");
308
}
309
public void actionPerformed(ActionEvent e)
310
{if(e.getSource()==b_add)
311
{String 名称=null;double 时间=0.0;
312
名称=名称文本框.getText();
313
try{时间=Double.valueOf(时间文本框.getText()).doubleValue();
314
}
315
catch(NumberFormatException ee)
316
{时间文本框.setText("请输入代表时间的实数");
317
}
318
节目 programme=new 节目(名称,时间);
319
节目清单.add(programme);
320
showing();
321
}
322
else if(e.getSource()==b_del)
323
{节目 待删除节目=null;
324
double time=Double.valueOf(删除文本框.getText()).doubleValue();
325
Iterator te=节目清单.iterator();
326
while(te.hasNext())
327
{节目 item=(节目)te.next();
328
if(Math.abs(item.time-time)<=0.000001d)
329
{待删除节目=item; }
330
}
331
if(待删除节目!=null) 节目清单.remove(待删除节目);
332
showing();
333
}
334
else if(e.getSource()==b_show)
335
{ showing();
336
}
337
}
338
void showing()
339
{ 显示区.setText(null);
340
Iterator iter=节目清单.iterator();
341
while(iter.hasNext())
342
{节目 item=(节目)iter.next();
343
显示区.append("节目名称:"+item.name+"演出时间: "+item.time);
344
显示区.append("\n");
345
}
346
}
347
}
348
public class Tree_3
349
{public static void main(String args[])
350
{ Win win=new Win();
351
win.setSize(500,250);win.setVisible(true);
352
win.addWindowListener(new WindowAdapter()
353
{public void windowClosing(WindowEvent e)
354
{System.exit(0);}});
355
}
356
}
357![]()
358
//例子10
359
import java.util.*;
360
class Student
361
{ int english=0; String name,number;
362
Student(String na,String nu,int e)
363
{english=e;name=na;number =nu;}
364
}
365
public class HT
366
{ public static void main(String args[])
367
{ Hashtable hashtable=new Hashtable();
368
hashtable.put("199901",new Student("199901","王小林",98));
369
hashtable.put("199902",new Student("199902","能林茂",70));
370
hashtable.put("199903",new Student("199903","多种林",93));
371
hashtable.put("199904",new Student("199904","围林蛤",46));
372
hashtable.put("199905",new Student("199905","夹贸林",77));
373
hashtable.put("199906",new Student("199906","噔林可",55));
374
hashtable.put("199907",new Student("199907","降王林",68));
375
hashtable.put("199908",new Student("199908","纠林咯",76));
376
Student stu=(Student)hashtable.get("199902");//检索一个元素。
377
System.out.println(stu.number+" "+stu.name+" "+stu.english);
378
hashtable.remove("199906"); //删除一个元素
379
System.out.println("散列表中现在含有:"+hashtable.size()+"个元素");
380
Enumeration enum=hashtable.elements();
381
while(enum.hasMoreElements()) //遍历当前散列表。
382
{Student s=(Student)enum.nextElement();
383
System.out.println(s.number+" "+s.name+" "+s.english);
384
}
385
}
386
}
387![]()
388
//例子11
389
import java.util.*;import java.awt.event.*;import java.awt.*;
390
import javax.swing.*;import java.io.*;
391
class 学生 extends JPanel
392
{String 学号,姓名;float 分数;
393
学生(String 学号,String 姓名,float 分数)
394
{this.学号=学号;this.姓名=姓名;this.分数=分数;
395
}
396
}
397
class ShowWin extends JFrame implements ActionListener
398
{ Hashtable hashtable=new Hashtable();
399
JTextField 学号文本框=new JTextField(),
400
姓名文本框=new JTextField(),
401
分数文本框=new JTextField(),
402
查询文本框=new JTextField();
403
JButton b_add=new JButton("添加成绩"),
404
b_show =new JButton("显示成绩");
405
JTextField 成绩显示条=new JTextField();
406
ShowWin()
407
{Container con=getContentPane();
408
JPanel 成绩输入区=new JPanel();
409
成绩输入区.setLayout(new GridLayout(5,2));
410
成绩输入区.add(new Label("成绩输入区:"));
411
成绩输入区.add(new Label());
412
成绩输入区.add(new Label("考生学号:"));
413
成绩输入区.add(学号文本框);
414
成绩输入区.add(new JLabel("考生姓名:"));
415
成绩输入区.add(姓名文本框);
416
成绩输入区.add(new Label("考生成绩:"));
417
成绩输入区.add(分数文本框);
418
成绩输入区.add(new Label("点击添加:"));
419
成绩输入区.add(b_add);
420
JPanel 查询显示区=new JPanel();
421
查询显示区.setLayout(new GridLayout(3,2));
422
查询显示区.add(new Label("成绩查询区:"));
423
查询显示区.add(new Label());
424
查询显示区.add(new Label("输入考生的学号:"));
425
查询显示区.add(查询文本框);
426
查询显示区.add(b_show);
427
查询显示区.add(成绩显示条);
428
JSplitPane split;
429
split=new JSplitPane(JSplitPane.VERTICAL_SPLIT,成绩输入区,查询显示区);
430
con.add(split,BorderLayout.CENTER);
431
con.add(new Label("成绩输入和查询系统"),BorderLayout.NORTH);
432
b_add.addActionListener(this);b_show.addActionListener(this);
433
}
434
public void actionPerformed(ActionEvent e)
435
{if(e.getSource()==b_add)
436
{String 学号=null,姓名=null;float 分数=0.0f;
437
try {学号=学号文本框.getText();
438
姓名=姓名文本框.getText();
439
}
440
catch(NullPointerException ee)
441
{ 学号文本框.setText("请输入学号");
442
姓名文本框.setText("请输入姓名");
443
}
444
try{分数=Float.valueOf(分数文本框.getText()).floatValue();}
445
catch(NumberFormatException ee)
446
{分数文本框.setText("请输入数字字符");}
447
学生 stu=new 学生(学号,姓名,分数);
448
hashtable.put(学号,stu);
449
try {FileOutputStream file=new FileOutputStream("score.txt");
450
ObjectOutputStream out=new ObjectOutputStream(file);
451
out.writeObject(hashtable); out.close();
452
}
453
catch(IOException event){}
454
}
455
else if(e.getSource()==b_show)
456
{ String temp=null;
457
temp=查询文本框.getText();
458
成绩显示条.setText(null);
459
try {FileInputStream come_in=new FileInputStream("score.txt");
460
ObjectInputStream in=new ObjectInputStream(come_in);
461
hashtable=(Hashtable)in.readObject();in.close();
462
}
463
catch(ClassNotFoundException event){}
464
catch(IOException event){System.out.println("文件无法读出");}
465
学生 s=(学生)hashtable.get(temp);
466
成绩显示条.setText("姓名:"+s.姓名+"学号:"+s.学号+"成绩:"+s.分数);
467
}
468
}
469
}
470
public class HT_2
471
{public static void main(String args[])
472
{ ShowWin win=new ShowWin();
473
win.setSize(100,100); win.setVisible(true);
474
win.addWindowListener(new WindowAdapter()
475
{public void windowClosing(WindowEvent e)
476
{System.exit(0);}});
477
}
478
}
479![]()
480
//例子12
481
import java.util.*;
482
class Example26_12
483
{public static void main(String args[])
484
{ Vector vector=new Vector(); Date date=new Date();
485
vector.add(new Integer(1));vector.add(new Float(3.45f));
486
vector.add(new Double(7.75));vector.add(new Boolean(true));
487
vector.add(date);
488
System.out.println(vector.size());
489
Integer number1=(Integer)vector.get(0);
490
System.out.println("向量的第1个元素: "+number1.intValue());
491
Float number2=(Float)vector.get(1);
492
System.out.println("向量的第2个元素: "+number2.floatValue());
493
Double number3=(Double)vector.get(2);
494
System.out.println("向量的第3个元素: "+number3.doubleValue());
495
Boolean number4=(Boolean)vector.get(3);
496
System.out.println("向量的第4个元素: "+number4.booleanValue());
497
date=(Date)vector.lastElement();
498
System.out.println("向量的第5个元素: "+date.toString());
499
if(vector.contains(date))
500
System.out.println("ok");
501
}
502
}
503![]()
504
//例子13
505
import java.applet.*;
506
import java.awt.*;import java.util.*;
507
import java.awt.event.*;
508
class Point
509
{int x,y;
510
Point(int x,int y)
511
{this.x=x;this.y=y;
512
}
513
}
514
public class Example26_13 extends Applet
515
implements MouseMotionListener,MouseListener
516
{ int x=-1,y=-1;
517
Vector v=null;int n=1;
518
public void init()
519
{ setBackground(Color.green);
520
addMouseMotionListener(this); addMouseListener(this);
521
v=new Vector();
522
}
523
public void paint(Graphics g)
524
{if(x!=-1&&y!=-1)
525
{ n=v.size();
526
for(int i=0;i<n-1;i++)
527
{Point p1=(Point)v.elementAt(i);
528
Point p2=(Point)v.elementAt(i+1);
529
g.drawLine(p1.x,p1.y,p2.x,p2.y);
530
}
531
}
532
533
}
534
public void mouseDragged(MouseEvent e)
535
{ x=(int)e.getX();y=(int)e.getY();
536
Point p=new Point(x,y);
537
v.addElement(p);
538
repaint();
539
}
540
public void mouseMoved(MouseEvent e)
541
{}
542
public void mousePressed(MouseEvent e){}
543
public void mouseReleased(MouseEvent e)
544
{v.removeAllElements();}
545
public void mouseEntered(MouseEvent e){}
546
public void mouseExited(MouseEvent e){}
547
public void mouseClicked(MouseEvent e){}
548
public void update(Graphics g)
549
{ paint(g);
550
}
551
}
552![]()
//例子12
import java.util.*;3
public class LinkListOne4
{public static void main(String args[])5
{ LinkedList mylist=new LinkedList();6
mylist.add("It"); //链表中的第一个节点。7
mylist.add("is"); //链表中的第二个节点。8
mylist.add("a"); //链表中的第三个节点。9
mylist.add("door"); //链表中的第四个节点。10
int number=mylist.size(); //获取链表的长度。11
for(int i=0;i<number;i++)12
{String temp=(String)mylist.get(i);13
System.out.println("第"+i+"节点中的数据:"+temp);14
} 15
}16
}17

18
//例子219
import java.util.*;20
public class LinkListTwo21
{public static void main(String args[])22
{ LinkedList mylist=new LinkedList();23
mylist.add("is"); mylist.add("a");24
int number=mylist.size();25
System.out.println("现在链表中有"+number+"个节点:");26
for(int i=0;i<number;i++)27
{String temp=(String)mylist.get(i);28
System.out.println("第"+i+"节点中的数据:"+temp);29
}30
mylist.addFirst("It");mylist.addLast("door");31
number=mylist.size(); 32
System.out.println("现在链表中有"+number+"个节点:");33
for(int i=0;i<number;i++)34
{String temp=(String)mylist.get(i);35
System.out.println("第"+i+"节点中的数据:"+temp);36
}37
mylist.remove(0);mylist.remove(1);38
mylist.set(0,"open");39
number=mylist.size();40
System.out.println("现在链表中有"+number+"个节点:");41
for(int i=0;i<number;i++)42
{String temp=(String)mylist.get(i);43
System.out.println("第"+i+"节点中的数据:"+temp);44
}45
}46
}47

48
//例子349
import java.util.*;50
class Student51
{String name ;int number;float score;52
Student(String name,int number,float score)53
{this.name=name;this.number=number;this.score=score;54
}55
}56
public class LinkListThree57
{public static void main(String args[])58
{ LinkedList mylist=new LinkedList();59
Student stu_1=new Student("赵好民" ,9012,80.0f),60
stu_2=new Student("钱小青" ,9013,90.0f), 61
stu_3=new Student("孙力枚" ,9014,78.0f),62
stu_4=new Student("周左右" ,9015,55.0f);63
mylist.add(stu_1); mylist.add(stu_2);64
mylist.add(stu_3); mylist.add(stu_4);65
Iterator iter=mylist.iterator();66
while(iter.hasNext())67
{ Student te=(Student)iter.next();68
System.out.println(te.name+" "+te.number+" "+te.score);69
}70
}71
}72

73
//例子474
import java.util.*;import java.awt.event.*;import java.awt.*;75
import javax.swing.*;import java.io.*;76
class 商品 extends Panel77
{String 代号,名称;int 库存;float 单价;78
商品(String 代号,String 名称,int 库存,float 单价)79
{this.代号=代号;this.名称=名称;this.库存=库存;this.单价=单价;80
}81
}82

83
class ShowWin extends JFrame implements ActionListener84
{ LinkedList goods_list=null;85
JTextField 代号文本框=new JTextField(),86
名称文本框=new JTextField(),87
库存文本框=new JTextField(),88
单价文本框=new JTextField(),89
删除文本框=new JTextField();90
JButton b_add=new JButton("添加商品"),91
b_del=new JButton("删除商品"),92
b_show =new JButton("显示商品清单");93
JTextArea 显示区=new JTextArea();94
ShowWin()95
{goods_list=new LinkedList();96
Container con=getContentPane(); 97
JScrollPane pane=new JScrollPane(显示区); 98
显示区.setEditable(false);99
JPanel save=new JPanel();save.setLayout(new GridLayout(5,2));100
save.add(new Label("输入代号:"));save.add(代号文本框);101
save.add(new Label("输入名称:"));save.add(名称文本框);102
save.add(new Label("输入库存:"));save.add(库存文本框);103
save.add(new Label("输入单价:"));save.add(单价文本框);104
save.add(new Label("点击添加:"));save.add(b_add);105
JPanel del=new JPanel();del.setLayout(new GridLayout(2,2));106
del.add(new Label("输入删除的代号:"));del.add(删除文本框);107
del.add(new Label("点击删除:"));del.add(b_del);108
JPanel show=new JPanel();show.setLayout(new BorderLayout());109
show.add(pane,BorderLayout.CENTER);show.add(b_show,BorderLayout.SOUTH);110
JSplitPane split_one,split_two;111
split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,save,del);112
split_two=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,split_one,show);113
con.add(split_two,BorderLayout.CENTER);114
b_add.addActionListener(this);b_del.addActionListener(this);115
b_show.addActionListener(this);116
}117
public void actionPerformed(ActionEvent e)118
{if(e.getSource()==b_add)119
{String daihao=null,mingcheng=null;int kucun=0;float danjia=0.0f;120
daihao=代号文本框.getText();mingcheng=名称文本框.getText();121
kucun=Integer.parseInt(库存文本框.getText());122
danjia=Float.valueOf(单价文本框.getText()).floatValue();123
商品 goods=new 商品(daihao,mingcheng,kucun,danjia);124
goods_list.add(goods);125
try {FileOutputStream file=new FileOutputStream("goods.txt");126
ObjectOutputStream out=new ObjectOutputStream(file);127
out.writeObject(goods_list);out.close();128
}129
catch(IOException event){}130
}131
else if(e.getSource()==b_del)132
{String daihao=删除文本框.getText();133
try {FileInputStream come_in=new FileInputStream("goods.txt");134
ObjectInputStream in=new ObjectInputStream(come_in);135
goods_list=(LinkedList)in.readObject();in.close();136
}137
catch(ClassNotFoundException event){}138
catch(IOException event){}139
for(int i=0;i<goods_list.size();i++)140
{商品 temp=(商品)goods_list.get(i);141
if(temp.代号.equals(daihao)) {goods_list.remove(i);}142
try {FileOutputStream file=new FileOutputStream("goods.txt");143
ObjectOutputStream out=new ObjectOutputStream(file);144
out.writeObject(goods_list);145
out.close();146
}147
catch(IOException event){}148
}149
}150
else if(e.getSource()==b_show)151
{ 显示区.setText(null);152
try {FileInputStream come_in=new FileInputStream("goods.txt");153
ObjectInputStream in=new ObjectInputStream(come_in);154
goods_list=(LinkedList)in.readObject();155
}156
catch(ClassNotFoundException event){}157
catch(IOException event){}158
Iterator iter=goods_list.iterator();159
while(iter.hasNext())160
{ 商品 te=(商品)iter.next();161
显示区.append("商品代号:"+te.代号+" ");162
显示区.append("商品名称:"+te.名称+" ");163
显示区.append("商品库存:"+te.库存+" ");164
显示区.append("商品单价:"+te.单价+" ");165
显示区.append("\n");166
}167
}168
}169
} 170
public class LinkListFour 171
{public static void main(String args[])172
{ ShowWin win=new ShowWin();173
win.setSize(100,100);174
win.setVisible(true);175
win.addWindowListener(new WindowAdapter()176
{public void windowClosing(WindowEvent e)177
{System.exit(0);}});178
}179
}180

181
//例子5182
import java.util.*;183
class StackOne184
{public static void main(String args[])185
{Stack mystack=new Stack();186
mystack.push(new Integer(1)); mystack.push(new Integer(2));187
mystack.push(new Integer(3)); mystack.push(new Integer(4));188
mystack.push(new Integer(5)); mystack.push(new Integer(6));189
while(!(mystack.empty()))190
{Integer temp=(Integer)mystack.pop();191
System.out.print(" "+temp.toString());}192
}193
}194

195
//例子6196
import java.util.*;197
class StackTwo198
{public static void main(String args[])199
{Stack mystack=new Stack();200
mystack.push(new Integer(1)); mystack.push(new Integer(1));201
int k=1;202
while(k<=10)203
for(int i=1;i<=2;i++)204
{Integer F1=(Integer)mystack.pop();int f1=F1.intValue();205
Integer F2=(Integer)mystack.pop();int f2=F2.intValue();206
Integer temp=new Integer(f1+f2);207
System.out.println(""+temp.toString()); 208
mystack.push(temp);mystack.push(F2);k++;209
}210
}211
}212

213
//例子7214
import java.util.*;215
class TreeOne216
{public static void main(String args[])217
{ TreeSet mytree=new TreeSet();218
mytree.add("boy");mytree.add("zoo");219
mytree.add("apple"); mytree.add("girl");220
Iterator te=mytree.iterator();221
while(te.hasNext())222
System.out.println(""+te.next());223
}224
}225

226
//例子8227
import java.util.*;import java.awt.*;228
class TreeTwo229
{public static void main(String args[])230
{ TreeSet mytree=new TreeSet(new Comparator()231
{public int compare(Object a,Object b)232
{Student stu1=(Student)a;Student stu2=(Student)b;233
return stu1.compareTo(stu2);}234
});235
Student st1,st2,st3,st4;236
st1=new Student(90,"zhan ying");st2=new Student(66,"wang heng");237
st3=new Student(86,"Liuh qing");st4=new Student(76,"yage ming");238
mytree.add(st1);mytree.add(st2);mytree.add(st3);mytree.add(st4);239
Iterator te=mytree.iterator();240
while(te.hasNext())241
{Student stu=(Student)te.next();242
System.out.println(""+stu.name+" "+stu.english);243
}244
}245
}246
class Student implements Comparable 247
{ int english=0;String name;248
Student(int e,String n)249
{english=e;name=n;250
}251
public int compareTo(Object b)252
{ Student st=(Student)b;253
return (this.english-st.english);254
}255
}256

257
//例子9 258
import java.util.*;import java.awt.event.*;259
import java.awt.*; 260
class 节目 implements Comparable261
{String name;double time;262
节目(String 名称,double 演出时间)263
{name=名称;time=演出时间;264
}265
public int compareTo(Object b)266
{节目 item=(节目)b;267
return (int)((this.time-item.time)*1000);268
}269
}270

271
class Win extends Frame implements ActionListener272
{ TreeSet 节目清单=null;273
TextField 名称文本框=new TextField(10),274
时间文本框=new TextField(5),275
删除文本框=new TextField(5);276
Button b_add=new Button("添加节目"),277
b_del=new Button("删除节目"),278
b_show =new Button("显示节目清单");279
TextArea 显示区=new TextArea();280
Win()281
{ 节目清单=new TreeSet(new Comparator()282
{public int compare(Object a,Object b)283
{节目 item_1=(节目)a;284
节目 item_2=(节目)b;285
return item_1.compareTo(item_2);286
}287
});288
Panel 节目单输入区=new Panel();289
节目单输入区.add(new Label("节目名称:"));290
节目单输入区.add(名称文本框);291
节目单输入区.add(new Label("演出时间:"));292
节目单输入区.add(时间文本框);293
节目单输入区.add(new Label("点击添加:"));294
节目单输入区.add(b_add);295
节目单输入区.add(b_show);296
Panel 节目单删除区=new Panel();297
节目单删除区.add(new Label("输入演出的时间:"));298
节目单删除区.add(删除文本框);299
节目单删除区.add(new Label("点击删除:"));300
节目单删除区.add(b_del);301
Panel 节目单显示区=new Panel();302
节目单显示区.add(显示区);303
显示区.setBackground(Color.pink); 304
b_add.addActionListener(this);b_del.addActionListener(this);305
b_show.addActionListener(this);306
add(节目单输入区,"North");add(节目单显示区,"Center");307
add(节目单删除区,"South");308
}309
public void actionPerformed(ActionEvent e)310
{if(e.getSource()==b_add)311
{String 名称=null;double 时间=0.0;312
名称=名称文本框.getText();313
try{时间=Double.valueOf(时间文本框.getText()).doubleValue();314
}315
catch(NumberFormatException ee)316
{时间文本框.setText("请输入代表时间的实数");317
}318
节目 programme=new 节目(名称,时间);319
节目清单.add(programme);320
showing();321
}322
else if(e.getSource()==b_del)323
{节目 待删除节目=null;324
double time=Double.valueOf(删除文本框.getText()).doubleValue();325
Iterator te=节目清单.iterator();326
while(te.hasNext())327
{节目 item=(节目)te.next();328
if(Math.abs(item.time-time)<=0.000001d)329
{待删除节目=item; }330
}331
if(待删除节目!=null) 节目清单.remove(待删除节目);332
showing();333
}334
else if(e.getSource()==b_show)335
{ showing();336
}337
}338
void showing()339
{ 显示区.setText(null);340
Iterator iter=节目清单.iterator();341
while(iter.hasNext())342
{节目 item=(节目)iter.next();343
显示区.append("节目名称:"+item.name+"演出时间: "+item.time);344
显示区.append("\n");345
}346
}347
} 348
public class Tree_3 349
{public static void main(String args[])350
{ Win win=new Win();351
win.setSize(500,250);win.setVisible(true);352
win.addWindowListener(new WindowAdapter()353
{public void windowClosing(WindowEvent e)354
{System.exit(0);}});355
}356
}357

358
//例子10359
import java.util.*;360
class Student 361
{ int english=0; String name,number;362
Student(String na,String nu,int e)363
{english=e;name=na;number =nu;}364
}365
public class HT366
{ public static void main(String args[])367
{ Hashtable hashtable=new Hashtable();368
hashtable.put("199901",new Student("199901","王小林",98));369
hashtable.put("199902",new Student("199902","能林茂",70));370
hashtable.put("199903",new Student("199903","多种林",93));371
hashtable.put("199904",new Student("199904","围林蛤",46));372
hashtable.put("199905",new Student("199905","夹贸林",77));373
hashtable.put("199906",new Student("199906","噔林可",55));374
hashtable.put("199907",new Student("199907","降王林",68));375
hashtable.put("199908",new Student("199908","纠林咯",76));376
Student stu=(Student)hashtable.get("199902");//检索一个元素。 377
System.out.println(stu.number+" "+stu.name+" "+stu.english);378
hashtable.remove("199906"); //删除一个元素379
System.out.println("散列表中现在含有:"+hashtable.size()+"个元素");380
Enumeration enum=hashtable.elements(); 381
while(enum.hasMoreElements()) //遍历当前散列表。382
{Student s=(Student)enum.nextElement();383
System.out.println(s.number+" "+s.name+" "+s.english);384
}385
} 386
}387

388
//例子11389
import java.util.*;import java.awt.event.*;import java.awt.*;390
import javax.swing.*;import java.io.*;391
class 学生 extends JPanel 392
{String 学号,姓名;float 分数;393
学生(String 学号,String 姓名,float 分数)394
{this.学号=学号;this.姓名=姓名;this.分数=分数;395
}396
}397
class ShowWin extends JFrame implements ActionListener398
{ Hashtable hashtable=new Hashtable();399
JTextField 学号文本框=new JTextField(),400
姓名文本框=new JTextField(),401
分数文本框=new JTextField(),402
查询文本框=new JTextField();403
JButton b_add=new JButton("添加成绩"),404
b_show =new JButton("显示成绩");405
JTextField 成绩显示条=new JTextField();406
ShowWin()407
{Container con=getContentPane(); 408
JPanel 成绩输入区=new JPanel();409
成绩输入区.setLayout(new GridLayout(5,2));410
成绩输入区.add(new Label("成绩输入区:"));411
成绩输入区.add(new Label());412
成绩输入区.add(new Label("考生学号:"));413
成绩输入区.add(学号文本框);414
成绩输入区.add(new JLabel("考生姓名:"));415
成绩输入区.add(姓名文本框);416
成绩输入区.add(new Label("考生成绩:"));417
成绩输入区.add(分数文本框);418
成绩输入区.add(new Label("点击添加:"));419
成绩输入区.add(b_add);420
JPanel 查询显示区=new JPanel();421
查询显示区.setLayout(new GridLayout(3,2));422
查询显示区.add(new Label("成绩查询区:"));423
查询显示区.add(new Label());424
查询显示区.add(new Label("输入考生的学号:"));425
查询显示区.add(查询文本框);426
查询显示区.add(b_show);427
查询显示区.add(成绩显示条);428
JSplitPane split;429
split=new JSplitPane(JSplitPane.VERTICAL_SPLIT,成绩输入区,查询显示区);430
con.add(split,BorderLayout.CENTER);431
con.add(new Label("成绩输入和查询系统"),BorderLayout.NORTH);432
b_add.addActionListener(this);b_show.addActionListener(this);433
}434
public void actionPerformed(ActionEvent e)435
{if(e.getSource()==b_add)436
{String 学号=null,姓名=null;float 分数=0.0f;437
try {学号=学号文本框.getText();438
姓名=姓名文本框.getText();439
}440
catch(NullPointerException ee)441
{ 学号文本框.setText("请输入学号");442
姓名文本框.setText("请输入姓名");443
}444
try{分数=Float.valueOf(分数文本框.getText()).floatValue();}445
catch(NumberFormatException ee)446
{分数文本框.setText("请输入数字字符");} 447
学生 stu=new 学生(学号,姓名,分数);448
hashtable.put(学号,stu);449
try {FileOutputStream file=new FileOutputStream("score.txt");450
ObjectOutputStream out=new ObjectOutputStream(file);451
out.writeObject(hashtable); out.close();452
}453
catch(IOException event){}454
}455
else if(e.getSource()==b_show)456
{ String temp=null;457
temp=查询文本框.getText();458
成绩显示条.setText(null);459
try {FileInputStream come_in=new FileInputStream("score.txt");460
ObjectInputStream in=new ObjectInputStream(come_in);461
hashtable=(Hashtable)in.readObject();in.close();462
}463
catch(ClassNotFoundException event){}464
catch(IOException event){System.out.println("文件无法读出");}465
学生 s=(学生)hashtable.get(temp);466
成绩显示条.setText("姓名:"+s.姓名+"学号:"+s.学号+"成绩:"+s.分数);467
}468
}469
} 470
public class HT_2471
{public static void main(String args[])472
{ ShowWin win=new ShowWin();473
win.setSize(100,100); win.setVisible(true);474
win.addWindowListener(new WindowAdapter()475
{public void windowClosing(WindowEvent e)476
{System.exit(0);}});477
}478
}479

480
//例子12481
import java.util.*;482
class Example26_12483
{public static void main(String args[])484
{ Vector vector=new Vector(); Date date=new Date();485
vector.add(new Integer(1));vector.add(new Float(3.45f)); 486
vector.add(new Double(7.75));vector.add(new Boolean(true));487
vector.add(date);488
System.out.println(vector.size());489
Integer number1=(Integer)vector.get(0);490
System.out.println("向量的第1个元素: "+number1.intValue());491
Float number2=(Float)vector.get(1);492
System.out.println("向量的第2个元素: "+number2.floatValue());493
Double number3=(Double)vector.get(2);494
System.out.println("向量的第3个元素: "+number3.doubleValue());495
Boolean number4=(Boolean)vector.get(3);496
System.out.println("向量的第4个元素: "+number4.booleanValue());497
date=(Date)vector.lastElement();498
System.out.println("向量的第5个元素: "+date.toString()); 499
if(vector.contains(date))500
System.out.println("ok"); 501
}502
} 503

504
//例子13505
import java.applet.*;506
import java.awt.*;import java.util.*;507
import java.awt.event.*;508
class Point509
{int x,y;510
Point(int x,int y)511
{this.x=x;this.y=y;512
}513
}514
public class Example26_13 extends Applet 515
implements MouseMotionListener,MouseListener516
{ int x=-1,y=-1;517
Vector v=null;int n=1;518
public void init()519
{ setBackground(Color.green);520
addMouseMotionListener(this); addMouseListener(this);521
v=new Vector(); 522
}523
public void paint(Graphics g)524
{if(x!=-1&&y!=-1)525
{ n=v.size();526
for(int i=0;i<n-1;i++)527
{Point p1=(Point)v.elementAt(i); 528
Point p2=(Point)v.elementAt(i+1); 529
g.drawLine(p1.x,p1.y,p2.x,p2.y);530
}531
}532
533
}534
public void mouseDragged(MouseEvent e)535
{ x=(int)e.getX();y=(int)e.getY();536
Point p=new Point(x,y);537
v.addElement(p);538
repaint();539
}540
public void mouseMoved(MouseEvent e)541
{} 542
public void mousePressed(MouseEvent e){} 543
public void mouseReleased(MouseEvent e)544
{v.removeAllElements();}545
public void mouseEntered(MouseEvent e){}546
public void mouseExited(MouseEvent e){}547
public void mouseClicked(MouseEvent e){}548
public void update(Graphics g)549
{ paint(g);550
} 551
}552




浙公网安备 33010602011771号