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.
importjava.io.*;importjava.util.*;classPrinter<T>{// Generic function to print elements of an arraypublicvoidprintArray(T[]array){for(Telement:array){System.out.println(element);}}}publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerscanner=newScanner(System.in);intn=scanner.nextInt();Integer[]intArray=newInteger[n];for(inti=0;i<n;i++){intArray[i]=scanner.nextInt();}n=scanner.nextInt();String[]stringArray=newString[n];for(inti=0;i<n;i++){stringArray[i]=scanner.next();}Printer<Integer>intPrinter=newPrinter<>();Printer<String>stringPrinter=newPrinter<>();intPrinter.printArray(intArray);stringPrinter.printArray(stringArray);if(Printer.class.getDeclaredMethods().length>1){System.out.println("The Printer class should only have 1 method named printArray.");}}}
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 →
My Java15 Solution: