-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericArray.java
More file actions
43 lines (36 loc) · 1.14 KB
/
GenericArray.java
File metadata and controls
43 lines (36 loc) · 1.14 KB
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
public class GenericArray<T> {
private T[] array;
@SuppressWarnings("unchecked")
public GenericArray(int size){
array = (T[]) new Object[size];
}
public void put(int index, T value){
array[index] = value;
}
public T get(int index){
return array[index];
}
public T[] rep(){
return array;
}
public String toString(){
String str = "";
for(int index = 0; index < array.length; index++){
str += array[index] + " ";
}
return str;
}
public static void main(String[] args){
GenericArray<Integer> gai = new GenericArray<Integer>(4);
//Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
// at GenericArray.main(GenericArray.java:23)
//Integer[] ia = (Integer[]) gai.rep();
Object[] oa = gai.rep();
for(int index = 0; index < 4; index++){
gai.put(index, index);
}
System.out.println(gai.toString());
//Integer[] oa = gai.rep();
//System.out.println(oa.toString());
}
}