@@ -90,7 +90,7 @@ public void addFirst(E e) {
90
90
public void addFirst(E e) {
91
91
if (e == null)//不允许放入null
92
92
throw new NullPointerException();
93
- elements\ [head = (head - 1) & (elements.length - 1)\ ] = e;//2.下标是否越界
93
+ elements[head = (head - 1) & (elements.length - 1)] = e;//2.下标是否越界
94
94
if (head == tail)//1.空间是否够用
95
95
doubleCapacity();//扩容
96
96
}
@@ -116,10 +116,10 @@ private void doubleCapacity() {
116
116
int newCapacity = n << 1;//原空间的2倍
117
117
if (newCapacity < 0)
118
118
throw new IllegalStateException("Sorry, deque too big");
119
- Object\[\ ] a = new Object\ [newCapacity\ ];
119
+ Object[ ] a = new Object[newCapacity];
120
120
System.arraycopy(elements, p, a, 0, r);//复制右半部分,对应上图中绿色部分
121
121
System.arraycopy(elements, 0, a, r, p);//复制左半部分,对应上图中灰色部分
122
- elements = (E\[\ ])a;
122
+ elements = (E[ ])a;
123
123
head = 0;
124
124
tail = n;
125
125
}
@@ -135,7 +135,7 @@ private void doubleCapacity() {
135
135
public void addLast(E e) {
136
136
if (e == null)//不允许放入null
137
137
throw new NullPointerException();
138
- elements\ [tail\ ] = e;//赋值
138
+ elements[tail] = e;//赋值
139
139
if ( (tail = (tail + 1) & (elements.length - 1)) == head)//下标越界处理
140
140
doubleCapacity();//扩容
141
141
}
@@ -149,10 +149,10 @@ public void addLast(E e) {
149
149
150
150
```
151
151
public E pollFirst() {
152
- E result = elements\ [head\ ];
152
+ E result = elements[head];
153
153
if (result == null)//null值意味着deque为空
154
154
return null;
155
- elements\[h\ ] = null;//let GC work
155
+ elements[h ] = null;//let GC work
156
156
head = (head + 1) & (elements.length - 1);//下标越界处理
157
157
return result;
158
158
}
@@ -165,10 +165,10 @@ public E pollFirst() {
165
165
```
166
166
public E pollLast() {
167
167
int t = (tail - 1) & (elements.length - 1);//tail的上一个位置是最后一个元素
168
- E result = elements\[t\ ];
168
+ E result = elements[t ];
169
169
if (result == null)//null值意味着deque为空
170
170
return null;
171
- elements\[t\ ] = null;//let GC work
171
+ elements[t ] = null;//let GC work
172
172
tail = t;
173
173
return result;
174
174
}
@@ -180,7 +180,7 @@ public E pollLast() {
180
180
181
181
```
182
182
public E peekFirst() {
183
- return elements\ [head\ ]; // elements\ [head\ ] is null if deque empty
183
+ return elements[head]; // elements[head] is null if deque empty
184
184
}
185
185
```
186
186
@@ -190,7 +190,7 @@ public E peekFirst() {
190
190
191
191
```
192
192
public E peekLast() {
193
- return elements\ [(tail - 1) & (elements.length - 1)\ ];
193
+ return elements[(tail - 1) & (elements.length - 1)];
194
194
}
195
195
```
196
196
0 commit comments