HackerRank Java- Generics




You are given code in the editor. Complete the code so that it prints the following lines:

1
2
3
Hello
World
Do not use method overloading because your answer will not be accepted.



  1. import java.io.IOException;
  2. import java.lang.reflect.Method;
  3. class Printer
  4. {
  5. public <T> void printArray(T[] array) {
  6. for (T item : array) {
  7. System.out.println(item);
  8. }
  9. }
  10. }
  11. public class Solution {
  12. public static void main( String args[] ) {
  13. Printer myPrinter = new Printer();
  14. Integer[] intArray = { 1, 2, 3 };
  15. String[] stringArray = {"Hello", "World"};
  16. myPrinter.printArray(intArray);
  17. myPrinter.printArray(stringArray);
  18. int count = 0;
  19. for (Method method : Printer.class.getDeclaredMethods()) {
  20. String name = method.getName();
  21. if(name.equals("printArray"))
  22. count++;
  23. }
  24. if(count > 1)System.out.println("Method overloading is not allowed!");
  25. }
  26. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube