-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericsEx.java
More file actions
67 lines (45 loc) · 1.21 KB
/
GenericsEx.java
File metadata and controls
67 lines (45 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* JAVA PROGRAM TO PRINT ARRAY OF ANYTYPE USING GENERICS
**/
import java.util.*;
class Printer <T>
{
void printArray(T a[] )
{
for(T i : a)
System.out.print(i+"\t");
System.out.println();
}
}
public class GenericsEx
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter no of elements in integer array ");
int n = scanner.nextInt();
System.out.println("Enter the elements");
Integer[] intArray = new Integer[n];
for (int i = 0; i < n; i++)
{
intArray[i] = scanner.nextInt();
}
System.out.println("Enter no of elements in a String Arrray");
n = scanner.nextInt();
String[] stringArray = new String[n];
System.out.println("Enter the Strings");
for (int i = 0; i < n; i++)
{
stringArray[i] = scanner.next();
}
System.out.println("The Entered values are ");
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.");
}
}
}