Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics Updated Solution in JAVA #239

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions P - Generics/Solution.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
class Solution {
private static <Element> void printArray(Element[] array) {
for (Element element : array) {
System.out.println(element);
}
}

public static void main(String args[]) {
Integer[] intArray = {1, 2, 3};
String[] stringArray = {"Hello", "World"};

printArray(intArray);
printArray(stringArray);
import java.util.*;

if (Solution.class.getDeclaredMethods().length > 2) {
System.out.println("You should only have 1 method named printArray.");
class Printer <T> {
public static <E> void printArray(E[] generic) {
for(E element : generic) {
System.out.println(element);
}
}
}
public class Generics {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Integer[] intArray = new Integer[n];
for (int i = 0; i < n; i++) {
intArray[i] = scanner.nextInt();
}
n = scanner.nextInt();
String[] stringArray = new String[n];
for (int i = 0; i < n; i++) {
stringArray[i] = scanner.next();
}
Printer<Integer> intPrinter = new Printer<Integer>();
Printer<String> stringPrinter = new Printer<String>();
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.");
}
}
}