public class GenericMethodWithTwoParameters
1: //GenericMethodWithTwoParameters.java
3: public class GenericMethodWithTwoParameters
4: {
5: //Generic method to determine if an object is in an array
6: public static <ValueType, ArrayType extends ValueType> boolean isIn(
7: ValueType value,
8: ArrayType[] array
9: )
10: {
11: for (int i = 0; i < array.length; i++)
12: {
13: if (value.equals(array[i]))
14: {
15: return true;
16: }
17: }
18: return false;
19: }
21: public static void main(String args[])
22: {
23: System.out.println();
25: //Use isIn() on Integers ...
26: Integer nums[] =
27: {
28: 1, 2, 3, 4, 5
29: };
30: if (isIn(2, nums))
31: {
32: System.out.println("2 is in nums");
33: }
34: if (!isIn(7, nums))
35: {
36: System.out.println("7 is not in nums");
37: }
39: System.out.println();
41: //Use isIn() on Strings ...
42: String strs[] =
43: {
44: "one", "two", "three", "four", "five"
45: };
46: if (isIn("two", strs))
47: {
48: System.out.println("two is in strs");
49: }
50: if (!isIn("seven", strs))
51: {
52: System.out.println("seven is not in strs");
53: }
55: //Opps! Won't compile! Types must be compatible.
56: //if(isIn("two", nums))
57: // System.out.println("two is in strs");
58: }
59: }