-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathIntArrays.java
More file actions
43 lines (35 loc) · 927 Bytes
/
IntArrays.java
File metadata and controls
43 lines (35 loc) · 927 Bytes
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
// Concrete implementation built atop skeletal implementation - Page83
package effectivejava.chapter4.item18;
import java.util.AbstractList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class IntArrays {
static List<Integer> intArrayAsList(final int[] a) {
if (a == null)
throw new NullPointerException();
return new AbstractList<Integer>() {
public Integer get(int i) {
return a[i]; // Autoboxing (Item 5)
}
@Override
public Integer set(int i, Integer val) {
int oldVal = a[i];
a[i] = val; // Auto-unboxing
return oldVal; // Autoboxing
}
public int size() {
return a.length;
}
};
}
public static void main(String[] args) {
int[] a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = i;
List<Integer> list = intArrayAsList(a);
Collections.shuffle(list);
System.out.println(list);
HashMap h = null;
}
}