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

2

3

4



5



6

7

8

9

10

11

12



13

14

15

16

17

18

19

20

21



22



23

24

25

26

27



28

29

30

31

32

33

34



35

36

37

38

39

40

41

42



43

44

45

46

47

48

49

50

51



52

53



54

55

56

57



58



59

60

61

62

63

64

65

66

67



68

69

70

71

72

73

74

75

76

77



78

79



80

81

82

83

84



85

86

87

88

89

90

91

92

93

94

95



96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118



119



120

121

122

123

124

125



126

127

128

129



130

131

132



133



134

135

136

137



138



139

140



141



142



143

144

145

146

147



148

149

150

151



152



153

154

155

156



157



158

159

160



161

162

163

164

165

166

167

168

169

170

171



172



173

174

175

176



177



178

179

180

181

182

183

184



185



186

187

188

189

190



191

192

193

194

195

196

197

198



199



200

201

202

203

204



205

206

207

208

209

210

211

212

213

214

215

216



217



218

219

220

221

222

223

224

225

226

227

228

229



230



231



232



233

234

235

236

237

238

239

240

241



242

243

244

245

246

247



248

249



250

251

252



253

254

255

256

257

258

259

260

261



262

263



264

265

266



267

268

269

270

271

272



273

274

275

276

277

278

279

280

281



282



283



284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310



311



312

313



314

315

316



317

318

319

320

321

322

323



324

325

326

327



328

329



330

331

332

333

334

335



336

337

338

339



340

341

342



343

344

345

346

347

348

349



350



351

352

353



354



355

356

357

358

359

360

361



362

363



364

365

366



367



368

369

370

371

372

373

374

375

376

377

378

379

380

381

382



383

384

385

386

387

388

389

390

391

392



393

394



395

396

397

398



399

400

401

402

403

404

405

406

407



408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435



436



437



438

439

440

441



442

443

444



445

446



447

448

449



450

451

452

453



454

455

456



457

458

459



460

461

462

463



464



465

466

467

468

469

470

471



472



473

474

475



476



477

478

479

480

481

482

483



484



485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509



510

511



512

513

514

515

516



517

518

519



520

521

522

523

524



525



526

527



528

529

530

531

532

533

534

535



536

537

538

539

540

541



542



543

544



545



546



547



548

549



550

551

552
