|
1 |
| -package com.coding.basic.impl; |
2 |
| - |
3 |
| -import java.util.Arrays; |
4 |
| - |
5 |
| -import com.coding.basic.Iterator; |
6 |
| -import com.coding.basic.List; |
7 |
| - |
8 |
| -/** |
9 |
| - * |
10 |
| - * @描述: ArrayList简单实现 |
11 |
| - * @作者:240094626 |
12 |
| - * @创建日期:2017-2-20 |
13 |
| - */ |
14 |
| -public class ArrayList implements List { |
15 |
| - |
16 |
| - |
17 |
| - /** |
18 |
| - * @comment:元素数组 |
19 |
| - */ |
20 |
| - private Object data[] = null; |
21 |
| - |
22 |
| - /** |
23 |
| - * @comment:数组元素个数 |
24 |
| - */ |
25 |
| - private int size = 0; |
26 |
| - |
27 |
| - /** |
28 |
| - * 无参构造函数,初始化容量为10的空列表 |
29 |
| - */ |
30 |
| - public ArrayList(){ |
31 |
| - this(10); |
32 |
| - } |
33 |
| - |
34 |
| - /** |
35 |
| - * @param length |
36 |
| - * 构造函数,初始化容量为length的空列表 |
37 |
| - */ |
38 |
| - public ArrayList(int length){ |
39 |
| - if(length < 0){ |
40 |
| - throw new IllegalArgumentException("初始容量参数非法:"+length); |
41 |
| - } |
42 |
| - data = new Object[length]; |
43 |
| - } |
44 |
| - |
45 |
| - |
46 |
| - /** |
47 |
| - * @createTime: 2017-2-21 下午1:32:28 |
48 |
| - * @param length |
49 |
| - * @return:void |
50 |
| - * @comment:列表结构扩展容量,每次增加原来的1/2容量 |
51 |
| - */ |
52 |
| - private void grow(int length){ |
53 |
| - int oldLength = data.length; |
54 |
| - if(length > oldLength){ |
55 |
| - Object oldData[] = data; |
56 |
| - int newLength = oldLength*3/2 + 1; |
57 |
| - if(newLength < length){ |
58 |
| - newLength = length; |
59 |
| - } |
60 |
| - data = new Object[newLength]; |
61 |
| - System.arraycopy(oldData, 0, data, 0, oldLength); |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - /** |
66 |
| - * @createTime: 2017-2-21 下午1:32:05 |
67 |
| - * @param index |
68 |
| - * @return:void |
69 |
| - * @comment:检验下标参数是否超限 |
70 |
| - */ |
71 |
| - private void check(int index) { |
72 |
| - if( index >= size){ |
73 |
| - throw new IndexOutOfBoundsException("Index:"+index+",size:"+size); |
74 |
| - } |
75 |
| - } |
76 |
| - |
77 |
| - @Override |
78 |
| - public void add(Object o) { |
79 |
| - grow(size+1); |
80 |
| - data[size++]=o; |
81 |
| - } |
82 |
| - |
83 |
| - @Override |
84 |
| - public void add(int index, Object o) { |
85 |
| - if( index > size || index < 0){ |
86 |
| - throw new IndexOutOfBoundsException("Index:"+index); |
87 |
| - } |
88 |
| - grow(size+1); |
89 |
| - System.arraycopy(data, index, data, index+1, size-index); |
90 |
| - data[index] = o; |
91 |
| - size++; |
92 |
| - |
93 |
| - } |
94 |
| - |
95 |
| - @Override |
96 |
| - public Object get(int index) { |
97 |
| - check(index); |
98 |
| - return data[index]; |
99 |
| - } |
100 |
| - |
101 |
| - |
102 |
| - |
103 |
| - @Override |
104 |
| - public Object remove(int index) { |
105 |
| - check(index); |
106 |
| - Object remove = data[index]; |
107 |
| - System.arraycopy(data, index+1, data, index, size-index); |
108 |
| - data[--size] = null; |
109 |
| - return remove; |
110 |
| - } |
111 |
| - |
112 |
| - @Override |
113 |
| - public int size() { |
114 |
| - return size; |
115 |
| - } |
116 |
| - |
117 |
| - |
118 |
| - @Override |
119 |
| - public String toString() { |
120 |
| - return "ArrayList [data=" + Arrays.toString(data) + ", size=" + size |
121 |
| - + "]"; |
122 |
| - } |
123 |
| - |
124 |
| - public Iterator iterator(){ |
125 |
| - return new ArrayListIterator(); |
126 |
| - } |
127 |
| - |
128 |
| - /** |
129 |
| - * @描述: 简单实现迭代器 |
130 |
| - * @作者:240094626 |
131 |
| - * @创建日期:2017-2-21 |
132 |
| - */ |
133 |
| - private class ArrayListIterator implements Iterator{ |
134 |
| - |
135 |
| - /** |
136 |
| - * @column:index |
137 |
| - * @comment:当前位置下标 |
138 |
| - */ |
139 |
| - private int index; |
140 |
| - |
141 |
| - /** |
142 |
| - * 无参构造,初始化迭代器的下标为0 |
143 |
| - */ |
144 |
| - public ArrayListIterator(){ |
145 |
| - index = 0; |
146 |
| - } |
147 |
| - |
148 |
| - @Override |
149 |
| - public boolean hasNext() { |
150 |
| - if(index < size){ |
151 |
| - return true; |
152 |
| - } |
153 |
| - return false; |
154 |
| - } |
155 |
| - |
156 |
| - @Override |
157 |
| - public Object next() { |
158 |
| - Object o = get(index++); |
159 |
| - return o; |
160 |
| - } |
161 |
| - |
162 |
| - } |
163 |
| - |
164 |
| -} |
165 |
| - |
166 |
| - |
| 1 | +package com.coding.basic.impl; |
| 2 | + |
| 3 | +import com.coding.basic.Iterator; |
| 4 | +import com.coding.basic.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * |
| 8 | + * @描述: ArrayList简单实现 |
| 9 | + * @作者:240094626 |
| 10 | + * @创建日期:2017-2-20 |
| 11 | + */ |
| 12 | +public class ArrayList implements List { |
| 13 | + |
| 14 | + /** |
| 15 | + * @comment:元素数组 |
| 16 | + */ |
| 17 | + private Object data[] = null; |
| 18 | + |
| 19 | + /** |
| 20 | + * @comment:数组元素个数 |
| 21 | + */ |
| 22 | + private int size = 0; |
| 23 | + |
| 24 | + /** |
| 25 | + * 无参构造函数,初始化容量为10的空列表 |
| 26 | + */ |
| 27 | + public ArrayList(){ |
| 28 | + this(10); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param length |
| 33 | + * 构造函数,初始化容量为length的空列表 |
| 34 | + */ |
| 35 | + public ArrayList(int length){ |
| 36 | + if(length < 0){ |
| 37 | + throw new IllegalArgumentException("初始容量参数非法:"+length); |
| 38 | + } |
| 39 | + data = new Object[length]; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * @createTime: 2017-2-21 下午1:32:28 |
| 45 | + * @param length |
| 46 | + * @return:void |
| 47 | + * @comment:列表结构扩展容量,每次增加原来的1/2容量 |
| 48 | + */ |
| 49 | + private void grow(int length){ |
| 50 | + int oldLength = data.length; |
| 51 | + if(length > oldLength){ |
| 52 | + Object oldData[] = data; |
| 53 | + int newLength = oldLength*3/2 + 1; |
| 54 | + if(newLength < length){ |
| 55 | + newLength = length; |
| 56 | + } |
| 57 | + data = new Object[newLength]; |
| 58 | + System.arraycopy(oldData, 0, data, 0, oldLength); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @createTime: 2017-2-21 下午1:32:05 |
| 64 | + * @param index |
| 65 | + * @return:void |
| 66 | + * @comment:检验下标参数是否超限 |
| 67 | + */ |
| 68 | + private void check(int index) { |
| 69 | + if( index >= size){ |
| 70 | + throw new IndexOutOfBoundsException("Index:"+index+",size:"+size); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void add(Object o) { |
| 76 | + grow(size+1); |
| 77 | + data[size++]=o; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void add(int index, Object o) { |
| 82 | + if( index > size || index < 0){ |
| 83 | + throw new IndexOutOfBoundsException("Index:"+index+",size:"+size); |
| 84 | + } |
| 85 | + grow(size+1); |
| 86 | + System.arraycopy(data, index, data, index+1, size-index); |
| 87 | + data[index] = o; |
| 88 | + size++; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Object get(int index) { |
| 94 | + check(index); |
| 95 | + return data[index]; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + @Override |
| 101 | + public Object remove(int index) { |
| 102 | + check(index); |
| 103 | + Object remove = data[index]; |
| 104 | + System.arraycopy(data, index+1, data, index, size-index); |
| 105 | + data[--size] = null; |
| 106 | + return remove; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int size() { |
| 111 | + return size; |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + @Override |
| 116 | + public String toString() { |
| 117 | + StringBuilder sb = new StringBuilder(); |
| 118 | + for(int i =0; i<size ;i++){ |
| 119 | + if(i > 0){ |
| 120 | + sb.append(","); |
| 121 | + } |
| 122 | + sb.append(data[i]); |
| 123 | + } |
| 124 | + return String.format("ArrayList {data=[%s], size=%d}", sb.toString(),size); |
| 125 | + } |
| 126 | + |
| 127 | + public Iterator iterator(){ |
| 128 | + return new ArrayListIterator(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @描述: 简单实现迭代器 |
| 133 | + * @作者:240094626 |
| 134 | + * @创建日期:2017-2-21 |
| 135 | + */ |
| 136 | + private class ArrayListIterator implements Iterator{ |
| 137 | + |
| 138 | + /** |
| 139 | + * @column:index |
| 140 | + * @comment:当前位置下标 |
| 141 | + */ |
| 142 | + private int index; |
| 143 | + |
| 144 | + /** |
| 145 | + * 无参构造,初始化迭代器的下标为0 |
| 146 | + */ |
| 147 | + public ArrayListIterator(){ |
| 148 | + index = 0; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public boolean hasNext() { |
| 153 | + if(index < size){ |
| 154 | + return true; |
| 155 | + } |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public Object next() { |
| 161 | + Object o = get(index++); |
| 162 | + return o; |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | +} |
| 168 | + |
| 169 | + |
0 commit comments