-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path74_Generics
More file actions
275 lines (230 loc) · 7.05 KB
/
74_Generics
File metadata and controls
275 lines (230 loc) · 7.05 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//generics= enable types(classes and interfaces) to be parameters when defining:
// classes, interfaces and method
// a benefit is to eliminate the need to create multiple versions
// of methods or classes for various data types.
// Use 1 version for all reference data types
---Draft idea---
<Main.java>
import javax.swing.*;
public class Main {
public static void main(String[] args){
Integer[] intArray = {1,2,3,4,5};
Double[] doubleArray = {5.5, 4.4, 3.3, 2.2, 1.1};
Character[] charArray = {'H', 'E', 'L', 'L', 'O'};
String[] stringArray = {"B","Y","E"};
displayArray(intArray);
displayArray(doubleArray);
displayArray(charArray);
displayArray(stringArray);
}
public static void displayArray(Integer[] array){
for(Integer x: array){
System.out.print(x+" ");
}
System.out.println();
}
public static void displayArray(Double[] array){
for(Double x: array){
System.out.print(x+" ");
}
System.out.println();
}
public static void displayArray(Character[] array){
for(Character x: array){
System.out.print(x+" ");
}
System.out.println();
}
public static void displayArray(String[] array){
for(String x: array){
System.out.print(x+" ");
}
System.out.println();
}
}
---Apply Generics---
public class Main {
public static void main(String[] args){
Integer[] intArray = {1,2,3,4,5};
Double[] doubleArray = {5.5, 4.4, 3.3, 2.2, 1.1};
Character[] charArray = {'H', 'E', 'L', 'L', 'O'};
String[] stringArray = {"B","Y","E"};
}
public static <Thing> void displayArray(Thing[] array){ //Apply generic
for(Thing x: array){
System.out.print(x+" ");
}
System.out.println();
}
}
---To print the first element of each array---
public class Main {
public static void main(String[] args){
Integer[] intArray = {1,2,3,4,5};
Double[] doubleArray = {5.5, 4.4, 3.3, 2.2, 1.1};
Character[] charArray = {'H', 'E', 'L', 'L', 'O'};
String[] stringArray = {"B","Y","E"};
System.out.println(getFirst(intArray));
System.out.println(getFirst(doubleArray));
System.out.println(getFirst(charArray));
System.out.println(getFirst(stringArray));
}
public static <Thing> void displayArray(Thing[] array){ //Apply generic
for(Thing x: array){
System.out.print(x+" ");
}
System.out.println();
}
public static <Thing> Thing getFirst(Thing[] array){//no void because returning smtg
return array[0];}
}
---To create a method that accepts all data type (DRAW)---
public class Main {
public static void main(String[] args) {
Player player = new Player();
Enemy enemy = new Enemy();
Item item = new Item();
Tree tree = new Tree();
draw(player);
draw(enemy);
draw(item);
draw(tree);
}
public static <Thing> void draw (Thing x){
x.draw();
}
}
===Generic CLasses===
--ori--
public class Main {
public static void main(String[] args) {
MyIntegerClass myInt = new MyIntegerClass(1);
MyDoubleClass myDouble = new MyDoubleClass(3.14);
MyCharacterClass myChar = new MyCharacterClass('@');
MyStringClass myString = new MyStringClass("Hello");
System.out.println(myInt.getValue());
System.out.println(myDouble.getValue());
System.out.println(myChar.getValue());
System.out.println(myString.getValue());
}
}
<MyIntegerClass.java>
public class MyIntegerClass {
Integer x;
MyIntegerClass(Integer x){
this.x = x;
}
public Integer getValue(){
return x;
}
}
<MyDoubleClass.java>
public class MyDoubleClass {
Double x;
MyDoubleClass(Double x){
this.x = x;
}
public Double getValue(){
return x;
}
}
<MyCharacterClass.java>
public class MyCharacterClass {
Character x;
MyCharacterClass(Character x){
this.x = x;
}
public Character getValue(){
return x;
}
}
<MyStringClass.java>
public class MyStringClass {
String x;
MyStringClass(String x){
this.x = x;
}
public String getValue(){
return x;
}
}
--apply Generic Classs--
<Main.java>
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
MyGenericClass<Integer> myInt = new MyGenericClass(1);
MyGenericClass<Double> myDouble = new MyGenericClass(3.14);
MyGenericClass<Character> myChar = new MyGenericClass('@');
MyGenericClass<String> myString = new MyGenericClass("Hello");
ArrayList<String> myFriends = new ArrayList<>();
System.out.println(myInt.getValue());
System.out.println(myDouble.getValue());
System.out.println(myChar.getValue());
System.out.println(myString.getValue());
}
}
<MyGenericClass.java>
public class MyGenericClass <Thing>{
<Thing> x;
MyGenericClass(Thing x){
this.x = x;
}
public Thing getValue(){
return x;
}
}
--To return the 2nd item--look like hashmap
<Main.java>
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
MyGenericClass<Integer,Integer> myInt = new MyGenericClass(1,9);
MyGenericClass<Double,Double> myDouble = new MyGenericClass(3.14,1.01);
MyGenericClass<Character,Character> myChar = new MyGenericClass('@','$');
MyGenericClass<String,String> myString = new MyGenericClass("Hello",'!');
ArrayList<String> myFriends = new ArrayList<>();
System.out.println(myInt.getValue());
System.out.println(myDouble.getValue());
System.out.println(myChar.getValue());
System.out.println(myString.getValue());
}
}
<MyGenericClass.java>
public class MyGenericClass <Thing, Thing2>{
Thing x;
Thing2 y;
MyGenericClass(Thing x, Thing2 y){
this.x = x;
this.y = y;
}
public Thing2 getValue(){
return y;
}
}
---bounded types---
//bounded types = you can create the objects of a generic class to have data
// of specific derived types ex.Number
<Main.java>
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
MyGenericClass<Integer,Integer> myInt = new MyGenericClass(1,9);
MyGenericClass<Double,Double> myDouble = new MyGenericClass(3.14,1.01);
System.out.println(myInt.getValue());
System.out.println(myDouble.getValue());
}
}
<MyGenericClass.java>
public class MyGenericClass <Thing extends Number, Thing2 extends Number>{
//Only Number Class can be passed in
Thing x;
Thing2 y;
MyGenericClass(Thing x, Thing2 y){
this.x = x;
this.y = y;
}
public Thing2 getValue(){
return y;
}
}