We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int t = sc.nextInt();
// Peek the next token to check if it is an int
if (sc.hasNextInt()) {
Integer[] arr = new Integer[t];
for (int i = 0; i < t; i++) {
if (sc.hasNextInt()) {
arr[i] = sc.nextInt();
}
}
printArray(arr);
} else {
String[] arr = new String[t];
for (int i = 0; i < t; i++) {
if (sc.hasNext()) {
arr[i] = sc.next();
}
}
printArray(arr);
}
}
sc.close();
}
public static <T> void printArray(T[] array) {
for (T item : array) {
System.out.println(item);
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 21: Generics
You are viewing a single comment's thread. Return to all comments →
import java.util.*;
public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
}