-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerics.java
More file actions
50 lines (42 loc) · 1.15 KB
/
Generics.java
File metadata and controls
50 lines (42 loc) · 1.15 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
import com.sun.istack.internal.NotNull;
import java.util.ArrayList;
import java.util.Objects;
/**
* Created by Admin on 12-04-2017.
*/
public class Generics<T> {
private final int DEFAULT_SIZE=10;
private final int INCREASE_IN_SIZE=5;
Object[] ts;
int size=DEFAULT_SIZE;
int currentSize;
public Generics(){
ts=new Object[DEFAULT_SIZE];
currentSize=0;
}
public void add(@NotNull T t){
if(currentSize<size){
ts[currentSize++] = t;
}else{
Object[] ts_new=new Object[size+INCREASE_IN_SIZE];
for(int i=0;i<size;i++){
ts_new[i]=ts[i];
}
ts=ts_new;
size+=INCREASE_IN_SIZE;
ts[currentSize++] = t;
}
}
public void print(){
for(int i=0;i<currentSize;i++){
System.out.println(ts[i].toString());
}
}
public static void main(String[] args){
Generics<Integer> generics=new Generics<>();
for(int i=0;i<100;i++){
generics.add(new Integer(i));
}
generics.print();
}
}