对象的容纳

生成能自动判别类型的Vector:

 1 import java.util.*;
2 class Gopher {
3 private int gopherNumber;
4 Gopher(int i) {
5 gopherNumber = i;
6 }
7 void print(String msg) {
8 if(msg != null)
9 System.out.println(msg);
10 System.out.println("Gopher number " + gopherNumber);
11 }
12 }
13 class GopherTrap {
14 static void caughtYa(Gopher g) {
15 g.print("Caught one!");
16 }
17 }
18 public class test {
19 private Vector v = new Vector();
20 public void addElement(Gopher m) {
21 v.addElement(m);
22 }
23 public Gopher elementAt(int index) {
24 return (Gopher)v.elementAt(index);
25 }
26 public int size() {
27 return v.size();
28 }
29 public static void main(String[] args) {
30 test gophers = new test();
31 for(int i = 0; i < 3; i++)
32 gophers.addElement(new Gopher(i));
33 for(int i = 0; i < gophers.size(); i++)
34 GopherTrap.caughtYa(gophers.elementAt(i));
35 }
36 }

下面的代码,因为在调用toString()方法的时候,其中的this那块会调用this.toString()。所以这段代码会无穷递归下去,然后导致Java崩溃。

 1 import java.util.Vector;
2
3 public class test {
4 public String toString() {
5 return "test address: " + this + "\n";
6 }
7 public static void main(String[] args) {
8 Vector v = new Vector();
9 for(int i = 0; i < 10; i++)
10 v.addElement(new test());
11 System.out.println(v);
12 }
13 }

BitSet的用法,要注意BitSet扩展的情况(初始化为64位):

 1 import java.util.*;
2 public class test {
3 public static void main(String[] args) {
4 Random rand = new Random();
5 // Take the LSB of nextInt():
6 byte bt = (byte)rand.nextInt();
7 BitSet bb = new BitSet();
8 for(int i = 7; i >=0; i--)
9 if(((1 << i) & bt) != 0)
10 bb.set(i);
11 else
12 bb.clear(i);
13 System.out.println("byte value: " + bt);
14 printBitSet(bb);
15 short st = (short)rand.nextInt();
16 BitSet bs = new BitSet();
17 for(int i = 15; i >=0; i--)
18 if(((1 << i) & st) != 0)
19 bs.set(i);
20 else
21 bs.clear(i);
22 System.out.println("short value: " + st);
23 printBitSet(bs);
24 int it = rand.nextInt();
25 BitSet bi = new BitSet();
26 for(int i = 31; i >=0; i--)
27 if(((1 << i) & it) != 0)
28 bi.set(i);
29 else
30 bi.clear(i);
31 System.out.println("int value: " + it);
32 printBitSet(bi);
33 // Test bitsets >= 64 bits:
34 BitSet b127 = new BitSet();
35 b127.set(127);
36 System.out.println("set bit 127: " + b127);
37 BitSet b255 = new BitSet(65);
38 b255.set(255);
39 System.out.println("set bit 255: " + b255);
40 BitSet b1023 = new BitSet(512);
41 // Without the following, an exception is thrown
42 // in the Java 1.0 implementation of BitSet:
43 // b1023.set(1023);
44 b1023.set(1024);
45 System.out.println("set bit 1023: " + b1023);
46 }
47 static void printBitSet(BitSet b) {
48 System.out.println("bits: " + b);
49 String bbits = new String();
50 for(int j = 0; j < b.size() ; j++)
51 bbits += (b.get(j) ? "1" : "0");
52 System.out.println("bit pattern: " + bbits);
53 }
54 }

Stack的用法:

 1 import java.util.*;
2 public class test {
3 static String[] months = {
4 "January", "February", "March", "April",
5 "May", "June", "July", "August", "September",
6 "October", "November", "December" };
7 public static void main(String[] args) {
8 Stack stk = new Stack();
9 for(int i = 0; i < months.length; i++)
10 stk.push(months[i] + " ");
11 System.out.println("stk = " + stk);
12 // Treating a stack as a Vector:
13 stk.addElement("The last line");
14 System.out.println("element 5 = " + stk.elementAt(5));
15 System.out.println("popping elements:");
16 while(!stk.empty())
17 System.out.println(stk.pop());
18 }
19 }

Dictionary的用法:

 1 import java.util.*;
2 public class test extends Dictionary {
3 private Vector keys = new Vector();
4 private Vector values = new Vector();
5 public int size() { return keys.size(); }
6 public boolean isEmpty() {
7 return keys.isEmpty();
8 }
9 public Object put(Object key, Object value) {
10 keys.addElement(key);
11 values.addElement(value);
12 return key;
13 }
14 public Object get(Object key) {
15 int index = keys.indexOf(key);
16 if(index == -1) return null;
17 return values.elementAt(index);
18 }
19 public Object remove(Object key) {
20 int index = keys.indexOf(key);
21 if(index == -1)
22 return null;
23 keys.removeElementAt(index);
24 Object returnval = values.elementAt(index);
25 values.removeElementAt(index);
26 return returnval;
27 }
28 public Enumeration keys() {
29 return keys.elements();
30 }
31 public Enumeration elements() {
32 return values.elements();
33 }
34 public static void main(String[] args) {
35 test aa = new test();
36 for(char c = 'a'; c <= 'z'; c++)
37 aa.put(String.valueOf(c),String.valueOf(c).toUpperCase());
38 char[] ca = { 'a', 'e', 'i', 'o', 'u' };
39 for(int i = 0; i < ca.length; i++)
40 System.out.println("Uppercase: " + aa.get(String.valueOf(ca[i])));
41 }
42 }

Hashtable的用法:

 1 import java.util.*;
2 class Counter {
3 int i = 1;
4 public String toString() {
5 return Integer.toString(i);
6 }
7 }
8 public class test {
9 public static void main(String[] args) {
10 Hashtable ht = new Hashtable();
11 for(int i = 0; i < 10000; i++) {
12 // Produce a number between 0 and 20:
13 Integer r = new Integer((int)(Math.random() * 20));
14 if(ht.containsKey(r))
15 ((Counter)ht.get(r)).i++;
16 else
17 ht.put(r, new Counter());
18 }
19 System.out.println(ht);
20 }
21 }

未完。

posted @ 2011-07-27 23:15  GG大婶  阅读(237)  评论(0编辑  收藏  举报