// Задание 1: обобщённый класс Box<T>
class Box<T> {
private T value;
public Box(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
public class Main {
// Задание 2: обобщённый метод поиска элемента в массиве
public static <T> boolean contains(T[] array, T element) {
for (T item : array) {
if (item == null ? element == null : item.equals(element)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
// Тест Box с тремя разными типами
Box<Integer> intBox = new Box<>(10);
System.out.println("Integer: " + intBox.getValue());
intBox.setValue(42);
System.out.println("Integer после set: " + intBox.getValue());
Box<String> strBox = new Box<>("Привет");
System.out.println("String: " + strBox.getValue());
strBox.setValue("Мир");
System.out.println("String после set: " + strBox.getValue());
Box<Double> dblBox = new Box<>(3.14);
System.out.println("Double: " + dblBox.getValue());
dblBox.setValue(2.71);
System.out.println("Double после set: " + dblBox.getValue());
// Тест метода contains
Integer[] numbers = {1, 2, 3, 4, 5};
String[] words = {"кот", "пёс", "мышь"};
System.out.println("\n3 в numbers: " + contains(numbers, 3)); // true
System.out.println("10 в numbers: " + contains(numbers, 10)); // false
System.out.println("\"пёс\" в words: " + contains(words, "пёс")); // true
System.out.println("\"лиса\" в words: " + contains(words, "лиса")); // false
}
}