Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package GenericBoxOfInteger_02;
- import java.util.ArrayList;
- import java.util.List;
- public class GenericBox<T> {
- private List<T> box;
- public GenericBox() {
- this.box = new ArrayList<>();
- }
- public void add(T element) {
- this.box.add(element);
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- for (T t : box) {
- sb.append(String.format("%s: %s", t.getClass(), t)).append(System.lineSeparator());
- }
- return sb.toString().replaceAll("class ", "");
- }
- }
- package GenericBoxOfInteger_02;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws NoSuchMethodException {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- GenericBox<Integer> box = new GenericBox<>();
- int line;
- for (int i = 0; i < n; i++) {
- line = Integer.parseInt(scanner.nextLine());
- box.add(line);
- }
- System.out.println(box);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment