user3713393
user3713393

Reputation: 3

Aid my use of generic data types

Please help me. This is a homework assignment and I cannot figure out why it wont work. When I call the linear Search method it says I am not passing the correct data type of T. T should be a generic data type so I do not understand why it is giving me a hard time. Please help!

    public static void main(String[] args) {
        int[] five= new int[5];
        int[] ten= new int[10];
        int[] fifteen= new int[15];
        fillArray(five);
        fillArray(ten);
        fillArray(fifteen);

        int result1 = linearSearch(five, 37);
        int result2 = linearSearch(ten, 37);
        int result3 = linearSearch(fifteen, 37);

        System.out.println(Arrays.toString(five) + " " + result1);
        System.out.println(Arrays.toString(ten) + " " + result2);
        System.out.println(Arrays.toString(fifteen) + " " + result3);

    }

//This method performs the linear search on the filled arrays and has to accept all data types.

    public static <T extends Comparable<T>> int linearSearch(T[] array, T data){
        int value = -1;  
          for(int i=0; i<array.length; i++){
            if (array[i]== data){
                value = i;
              }
          }
          return value;
        }

Upvotes: 0

Views: 76

Answers (2)

asteri
asteri

Reputation: 11592

Generics don't work with primitive types. You can only use actual objects. And unfortunately, auto-boxing won't magically work in this scenario.

You can get this to work and continue to use generics by using the wrapper object, Integer (assuming your fillArray method will work with an Integer object, since it's not posted in your example). e.g.,

public static void main(String[] args) {
    Integer[] five = new Integer[5];
    Integer[] ten = new Integer[10];
    Integer[] fifteen = new Integer[15];

    fillArray(five);
    fillArray(ten);
    fillArray(fifteen);

    int result1 = linearSearch(five, 37);
    int result2 = linearSearch(ten, 37);
    int result3 = linearSearch(fifteen, 37);

    System.out.println(Arrays.toString(five) + " " + result1);
    System.out.println(Arrays.toString(ten) + " " + result2);
    System.out.println(Arrays.toString(fifteen) + " " + result3);

}

If you're interested in some of the formal theory, you can read more about type parameters in JLS 4.5 - Parameterized Types. As you see in 4.5.1, it says that:

Type arguments may be either reference types or wildcards.

... which implicitly excludes primitive types. Couldn't find where in the JLS it actually explicitly forbids them, but I'm sure it's in there.

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201497

You can't do it with Generics, but you can with reflection! Like so,

public static int linearSearch(Object array,
    Object data) {
  int len = Array.getLength(array);

  for (int i = 0; i < len; i++) {
    if (Array.get(array, i).equals(data)) {
      return i;
    }
  }
  return -1;
}

public static void main(String[] args) {
  int[] five = new int[] { 1, 2, 37 };
  int[] ten = new int[] { 1, 37, 5 };
  float[] fifteen = new float[] { 1.0f, 2.2f, 0f, 4f };

  int result1 = linearSearch(five, 37);
  int result2 = linearSearch(ten, 37);
  int result3 = linearSearch(fifteen, 37);

  System.out.println(Arrays.toString(five) + " "
      + result1);
  System.out.println(Arrays.toString(ten) + " "
      + result2);
  System.out.println(Arrays.toString(fifteen) + " "
      + result3);
}

And the output is,

[1, 2, 37] 2
[1, 37, 5] 1
[1.0, 2.2, 0.0, 4.0] -1

Upvotes: 1

Related Questions