Day 21: Generics
Generics
Generic constructs are a very efficient way of reusing your code. In the same way you have parameters for a function, generics parameterize type and allow you to use apply the same interface, class, or method using different data types while still restricting operations to that data type (meaning that you still get strong type checking at compile time).
Generic implementation
public interface List<E> extends Collection<E>
public interface Map<K,V>
The letters enclosed between angle brackets (< and >) are type parameters and, like many things in programming, there is a convention behind them (remember, following conventions help us write clean, readable code!). The letters below are commonly-used generic type parameters:
E - Element
K - Key
V - Value
N - Number
T - Type (e.g.: data type)
S,U,V, etc. These are second, third, and fourth types for when T is already in use.
Aim
Hey CodingHumans today we're discussing Generics; be aware that not all languages support this construct, so fewer languages are enabled for this challenge.
Task
Write a single generic function named printArray; this function must take an array of generic elements as a parameter (the exception to this is C++, which takes a vector). The locked Solution class in your editor tests your function.
Note: You must use generics to solve this challenge. Do not write overloaded functions.
Input Style
The locked Solution class in your editor will pass different types of arrays to your printArray function.
Constraints
You must have exactly 1 function named printArray.
Output Style
Your printArray function should print each element of its generic array parameter on a new line.
Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.
Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.
HAPPY CODING 😁
Solution
( Java )
import java.util.*;
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.");
}
}
}
If you have any doubts regarding this problem or need the solution in other programming languages then leave a comment down below .