Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
43 lines (32 loc) · 1.39 KB

File metadata and controls

43 lines (32 loc) · 1.39 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.ArrayList;
import java.util.Arrays;
// Пустой объявленный класс, где Т обозначает тип, который будет заменен реальным типом.
public class GenericClass<T> {
// Объявляем объект (в данном случае массив) типа Т
private T[] obj;
//Передаем конструктору ссылку на объект типа Т
public GenericClass(T[] obj) {
this.obj = obj;
}
// Возвращаем объект типа Т
public T[] getObj() {
return obj;
}
// Получаем информацию о типе объекта (это пишу для себя)
public void showType() {
System.out.println(obj.getClass().getName());
}
// Массив в лист
public ArrayList<T> createArrayList() {
ArrayList<T> arrayList = new ArrayList<T>(Arrays.asList(obj));
System.out.println(arrayList);
return arrayList;
}
// Меняем элементы местами (проверку выхода за рамки массива не пишу, так как массив только из 2х элементов:)
public void changePositionOfElementsInArray() {
T x = obj[0];
obj[0] = obj[1];
obj[1] = x;
System.out.println(Arrays.toString(obj));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.