Skip to content

Commit 07d1c90

Browse files
committed
add
Signed-off-by: maskleo <[email protected]>
1 parent 6e5fc20 commit 07d1c90

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

Diff for: ch06/08_Array_Creation_and_Varargs.md

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
## 数组创建和可变参数
2+
3+
方便的变量表示法允许方法接受可变数量的参数并将它们打包到数组中,如 `1.4` 节所述。 这种表示法并不像你想要的那样方便,因为它创建的数组受到与其他数组相同的涉及到物化的问题的困扰。
4+
5+
`6-2`。 如何定义 `ArrayList`
6+
7+
```java
8+
import java.util.*;
9+
class ArrayList<E> extends AbstractList<E> implements RandomAccess {
10+
private E[] arr;
11+
private int size = 0;
12+
public ArrayList(int cap) {
13+
if (cap < 0)
14+
throw new IllegalArgumentException("Illegal Capacity: "+cap);
15+
arr = (E[])new Object[cap]; // unchecked cast
16+
}
17+
public ArrayList() { this(10); }
18+
public ArrayList(Collection<? extends E> c) { this(c.size()); addAll(c); }
19+
public void ensureCapacity(int mincap) {
20+
int oldcap = arr.length;
21+
if (mincap > oldcap) {
22+
int newcap = Math.max(mincap, (oldcap*3)/2+1);
23+
E[] oldarr = arr;
24+
arr = (E[])new Object[newcap]; // unchecked cast
25+
System.arraycopy(oldarr,0,arr,0,size);
26+
}
27+
}
28+
public int size() { return size; }
29+
private void checkBounds(int i, int size) {
30+
if (i < 0 || i >= size)
31+
throw new IndexOutOfBoundsException("Index: "+i+", Size: "+size);
32+
}
33+
public E get(int i) { checkBounds(i,size); return arr[i]; }
34+
public E set(int i, E elt) {
35+
checkBounds(i,size); E old = arr[i]; arr[i] = elt; return old;
36+
}
37+
public void add(int i, E elt) {
38+
checkBounds(i,size+1); ensureCapacity(size+1);
39+
System.arraycopy(arr,i,arr,i+1,size-i); arr[i] = elt; size++;
40+
}
41+
public E remove(int i) {
42+
checkBounds(i,size); E old = arr[i]; arr[i] = null; size--;
43+
System.arraycopy(arr,i+1,arr,i,size-i); return old;
44+
}
45+
public <T> T[] toArray(T[] a) {
46+
if (a.length < size)
47+
a = (T[])java.lang.reflect.Array. // unchecked cast
48+
newInstance(a.getClass().getComponentType(), size);
49+
System.arraycopy(arr,0,a,0,size);
50+
if (size < a.length) a[size] = null;
51+
return a;
52+
}
53+
public Object[] toArray() { return toArray(new Object[0]); }
54+
}
55+
```
56+
57+
`1.4` 节我们讨论了声明为的方法 `java.util.Arrays.asList` 如下:
58+
59+
```java
60+
public static <E> List<E> asList(E... arr)
61+
```
62+
63+
例如,这里有三个对这个方法的调用:
64+
65+
```java
66+
List<Integer> a = Arrays.asList(1, 2, 3);
67+
List<Integer> b = Arrays.asList(4, 5, 6);
68+
List<List<Integer>> x = Arrays.asList(a, b); // 通用数组创建
69+
```
70+
71+
回想一下,可变长度的参数列表是通过将参数打包到数组中并传递它来实现的。 因此这三个呼叫相当于以下内容:
72+
73+
```java
74+
List<Integer> a = Arrays.asList(new Integer[] { 1, 2, 3 });
75+
List<Integer> b = Arrays.asList(new Integer[] { 4, 5, 6 });
76+
List<List<Integer>> x = Arrays.asList(new List<Integer>[] { a, b }); // 通用数组创建
77+
```
78+
79+
前两个调用很好,但由于 `List<Integer>` 不是可重用的类型,所以第三次在编译时警告未经检查的泛型数组的创建。
80+
81+
```java
82+
VarargError.java:6: warning: [unchecked] unchecked generic array creation
83+
of type java.util.List<java.lang.Integer>[] for varargs parameter
84+
List<List<Integer>> x = Arrays.asList(a, b);
85+
```
86+
87+
此警告可能会造成混淆,特别是因为该源代码行不包含数组创建的显式实例!
88+
89+
如果您尝试创建泛型类型的列表,则会出现类似的问题。 这是一个使用 `Arrays.asList` 创建包含给定元素的长度列表的方法:
90+
91+
```java
92+
public static List<E> singleton(E elt) {
93+
return Arrays.asList(elt); // 通用数组创建
94+
}
95+
```
96+
97+
这也会产生警告,出于同样的原因可能会造成混淆。
98+
99+
通常,通用阵列创建报告错误。作为一种解决方法,可以创建一个可修饰类型的数组,并执行未经检查的转换。该变通方法不适用于可变参数使用中隐含的数组创建,因此在这种情况下,通用数组创建会发出警告而不是错误。通用数组创建警告就像未经检查的警告一样,因为它会使伴随泛型的铸铁保证失效。将前面的每个示例都作为未经检查的警告的结果,并不难,并创建一个使用可变参数的类似示例,其中发出通用数组创建警告。
100+
101+
在我们看来,可变参数提供的便利性超过了未经检查的警告中固有的危险,并且我们建议您在参数不可接受时避免使用可变参数。例如,在前面的两个例子中,我们不是使用 `Arrays.asList`,而是创建一个新的 `ArrayList` 并使用 `add`方法,即使这样做不太方便也不那么高效。
102+
103+
如果可变参数符号已被定义为将参数打包到列表而不是数组中,那么就不会出现通用数组创建警告和相关变通方法的需要,因为 `T ...` 等同于 `List<T>` 而不是 `T[]`。不幸的是,可变参数表示法是在完全理解这个问题之前设计的。
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+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+

0 commit comments

Comments
 (0)