|
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * ��ά����ʵ��Map,���dz��ȹ̶������Ҳ���Ч�ʲ��� |
5 | | - * |
6 | | - * @author yangqc |
7 | | - * |
| 5 | + * |
8 | 6 | * @param <K> |
9 | 7 | * @param <V> |
| 8 | + * @author yangqc |
10 | 9 | */ |
11 | 10 | public class AssociativeArray<K, V> { |
12 | | - private Object[][] pairs; |
13 | | - private int index; |
| 11 | + private Object[][] pairs; |
| 12 | + private int index; |
14 | 13 |
|
15 | | - public AssociativeArray(int length) { |
16 | | - pairs = new Object[length][2]; |
17 | | - } |
| 14 | + public AssociativeArray(int length) { |
| 15 | + pairs = new Object[length][2]; |
| 16 | + } |
18 | 17 |
|
19 | | - public void put(K key, V value) { |
20 | | - if (index >= pairs.length) { |
21 | | - throw new ArrayIndexOutOfBoundsException(); |
22 | | - } |
23 | | - pairs[index++] = new Object[] { key, value }; |
24 | | - } |
| 18 | + public void put(K key, V value) { |
| 19 | + if (index >= pairs.length) { |
| 20 | + throw new ArrayIndexOutOfBoundsException(); |
| 21 | + } |
| 22 | + pairs[index++] = new Object[]{key, value}; |
| 23 | + } |
25 | 24 |
|
26 | | - @SuppressWarnings("unchecked") |
27 | | - public V get(K key) { |
28 | | - for (int i = 0; i < index; i++) { |
29 | | - if (key.equals(pairs[i][0])) { |
30 | | - return (V) pairs[i][1]; |
31 | | - } |
32 | | - } |
33 | | - return null; |
34 | | - } |
| 25 | + @SuppressWarnings("unchecked") |
| 26 | + public V get(K key) { |
| 27 | + for (int i = 0; i < index; i++) { |
| 28 | + if (key.equals(pairs[i][0])) { |
| 29 | + return (V) pairs[i][1]; |
| 30 | + } |
| 31 | + } |
| 32 | + return null; |
| 33 | + } |
35 | 34 |
|
36 | | - public String toString() { |
37 | | - StringBuilder result = new StringBuilder(); |
38 | | - for (int i = 0; i < index; i++) { |
39 | | - result.append(pairs[i][0].toString()); |
40 | | - result.append(" : "); |
41 | | - result.append(pairs[i][1].toString()); |
42 | | - if (i < index - 1) { |
43 | | - result.append("\n"); |
44 | | - } |
45 | | - } |
46 | | - return result.toString(); |
47 | | - } |
| 35 | + @Override |
| 36 | + public String toString() { |
| 37 | + StringBuilder result = new StringBuilder(); |
| 38 | + for (int i = 0; i < index; i++) { |
| 39 | + result.append(pairs[i][0].toString()); |
| 40 | + result.append(" : "); |
| 41 | + result.append(pairs[i][1].toString()); |
| 42 | + if (i < index - 1) { |
| 43 | + result.append("\n"); |
| 44 | + } |
| 45 | + } |
| 46 | + return result.toString(); |
| 47 | + } |
48 | 48 |
|
49 | | - public static void main(String[] args) { |
50 | | - AssociativeArray<String, String> map = new AssociativeArray<String, String>( |
51 | | - 6); |
52 | | - map.put("sky", "bule"); |
53 | | - map.put("grass", "green"); |
54 | | - map.put("ocean", "dancing"); |
55 | | - map.put("tree", "tail"); |
56 | | - map.put("earth", "brown"); |
57 | | - map.put("sun", "warm"); |
58 | | - try { |
59 | | - map.put("extra", "object"); |
60 | | - } catch (ArrayIndexOutOfBoundsException e) { |
61 | | - System.out.println("Too mmany objects!"); |
62 | | - } |
63 | | - System.out.println(map); |
64 | | - System.out.println(map.get("ocean")); |
65 | | - } |
| 49 | + public static void main(String[] args) { |
| 50 | + AssociativeArray<String, String> map = new AssociativeArray<>(6); |
| 51 | + map.put("sky", "bule"); |
| 52 | + map.put("grass", "green"); |
| 53 | + map.put("ocean", "dancing"); |
| 54 | + map.put("tree", "tail"); |
| 55 | + map.put("earth", "brown"); |
| 56 | + map.put("sun", "warm"); |
| 57 | + try { |
| 58 | + map.put("extra", "object"); |
| 59 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 60 | + System.out.println("Too mmany objects!"); |
| 61 | + } |
| 62 | + System.out.println(map); |
| 63 | + System.out.println(map.get("ocean")); |
| 64 | + } |
66 | 65 | } |
0 commit comments