-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerics
More file actions
77 lines (56 loc) · 1.2 KB
/
generics
File metadata and controls
77 lines (56 loc) · 1.2 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Arrays;
public class main< T extends Number> {
private Object[] data;
private static int DEFAULT_SIZE=10;
private int size=0;
public main(){
this.data=new Object[DEFAULT_SIZE];
}
public void getList(main<? extends Number> list){
//if(<Number>){then you can pass number type only}
//if(<? extends Number>){then you can pass number and it's subclass type too }
}
public void add(T num){
if(isFull()){
reSize();
}
data[size++]=num;
}
private boolean isFull(){
return size==data.length;
}
public int size(){
return size;
}
public T remove(int index){
T removed=(T)(data[--size]);
return removed;
}
public void reSize(){
Object[] temp=new Object[data.length*2];
for(int i=0;i<data.length;i++){
temp[i]=data[i];
}
this.data=temp;
}
public void set(int index,T value){
data[index]=value;
}
public void isEmpty(){
if(size==0){
System.out.println("true");
}
else{
System.out.println("false");
}
}
public T get(int index){
return (T)(data[index]);
}
@Override
public String toString(){
return Arrays.toString(data);
}
public static void main(String[] args) {
}
}