最近看了下ArrayList的源碼,抽空根據(jù)ArrayList的底層結(jié)構(gòu)寫了一個功能簡單無泛型的自定義ArrayLsit,幫助自己更好理解ArrayList:,其實現(xiàn)的底層數(shù)據(jù)結(jié)構(gòu)為數(shù)Object組,代碼如下:
1
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
|
/** * 自己實現(xiàn)一個ArrayList * */ public class MyArrayList { private Object[] elementData; private int size; public int size(){ return size; } public boolean isEmpty(){ return size== 0 ; } //默認容量為10 public MyArrayList(){ this ( 10 ); } /** * 自定義容量 * @param initialCapacity */ public MyArrayList( int initialCapacity){ if (initialCapacity< 0 ){ try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } } elementData = new Object[initialCapacity]; } /** * 添加一個元素 * @param obj */ public void add(Object obj){ //數(shù)組擴容和數(shù)據(jù)的拷貝,重新new一個數(shù)組 if (size==elementData.length){ Object[] newArray = new Object[size* 2 + 1 ]; System.arraycopy(elementData, 0 , newArray, 0 , elementData.length); elementData = newArray; } elementData[size++]=obj; // size++; } /** * 通過索引獲取元素 * @param index * @return */ public Object get( int index){ rangeCheck(index); return elementData[index]; } /** * 通過索引刪除元素 * @param index */ public void remove( int index){ rangeCheck(index); int numMoved = size - index - 1 ; if (numMoved > 0 ){ System.arraycopy(elementData, index+ 1 , elementData, index, numMoved); } elementData[--size] = null ; // Let gc do its work } /** * 刪除對應(yīng)的元素(利用equal判斷元素是否一致) * @param obj */ public void remove(Object obj){ for ( int i= 0 ;i<size;i++){ if (get(i).equals(obj)){ //注意:底層調(diào)用的equals方法而不是==. remove(i); } } } /** * 設(shè)置索引對應(yīng)的元素 * @param index * @param obj * @return */ public Object set( int index,Object obj){ rangeCheck(index); Object oldValue = elementData[index]; elementData[index] = obj; return oldValue; } /** * 將元素插入對應(yīng)的位置 * @param index * @param obj */ public void add( int index,Object obj){ rangeCheck(index); ensureCapacity(); //數(shù)組擴容 System.arraycopy(elementData, index, elementData, index + 1 , size - index); elementData[index] = obj; size++; } /** * 數(shù)組擴容 */ private void ensureCapacity(){ //數(shù)組擴容和數(shù)據(jù)的拷貝 if (size==elementData.length){ Object[] newArray = new Object[size* 2 + 1 ]; System.arraycopy(elementData, 0 , newArray, 0 , elementData.length); // for(int i=0;i<elementData.length;i++){ // newArray[i] = elementData[i]; // } elementData = newArray; } } /** * 數(shù)組下標檢查 * @param index */ private void rangeCheck( int index){ if (index< 0 ||index>=size){ try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { MyArrayList list = new MyArrayList( 3 ); list.add( "333" ); list.add( "444" ); list.add( "5" ); list.add( "344433" ); list.add( "333" ); list.add( "333" ); for ( int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println( "------------------------------" ); list.remove( "444" ); list.add( 2 , "a" ); for ( int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); } } } |
測試結(jié)果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
333 444 5 344433 333 333 ------------------------------ 333 5 a 344433 333 333 |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/sinat_23092639/article/details/51352382