forked from igortereshchenko/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassWithGeneric.java
More file actions
45 lines (31 loc) · 1.03 KB
/
ClassWithGeneric.java
File metadata and controls
45 lines (31 loc) · 1.03 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
package LessonGeneric;
import java.util.ArrayList;
import java.util.List;
import Person.Person;
/**
* Created by student on 21.03.2018.
*/
public class ClassWithGeneric
{
public static <Type1> void genericPrinter(Type1 object){
System.out.println(object);
}
public static <Type2> int lengthCollection(List<Type2> list){
return list.size();
}
public static <T> String[] allToArrayOfStrings(ArrayList<T> list){ // public static String[] allToArrayOfStrings(ArrayList<? extends Object> list)
String[] array = new String[list.size()];
T variable;
for (int i = 0; i < list.size(); i++) {
array[i]=list.get(i).toString();
}
return array;
}
public static String[] personsToArrayOfStrings(ArrayList<? extends Person> list){
String [] result = new String[list.size()];
for (int i = 0; i <list.size() ; i++) {
result[i]= "Name :"+list.get(i).getName()+ " Age :"+list.get(i).getAge();
}
return result;
}
}