0

Okay, so I am trying to write a generic sorting package in Java (I'm taking an algorithms course and I see it as good practice, both in the algorithmic sense and in Java as I'm very new to the language). Anyway, I figure the best way to do this is to create a Sort class with multiple sorting methods within, such as insertionSort, mergeSort etc. If you feel there's a better way of doing this, please let me know as a comment as I am always open to suggestions for writing cleaner and more efficient code.

As for the question:

I have the backbone structure down and want to try coding the insertionSort method first. However, I have tried to pass it an integer array but am getting some problems. Firstly, with generics I understand that you have to use <Integer> as opposed to <int>, but for some reason if I create an array such as int[] arr = new int[]{..} and pass this in to the generic it does not work. How do I fix this without using Integer[] arr = new Integer[]{..}? I was under the impression that the compiler would box my int to Integer automatically?

My code:

public class Sort<T> {
    public T[] insertionSort(T[] array) {
        // do some stuff
        return array;
    }

    public static void main(String[] args) {
        int[] orig = new int[]{1,35,3,2,4634,2,46,7,33,56};
        Sort<Integer> sorter = new Sort<Integer>();
        sorter.insertionSort(orig);
    }
}
Keir Simmons
  • 1,634
  • 7
  • 21
  • 37
  • what does it mean "it does not work"? can you please give us the stack trace – PrR3 Mar 06 '14 at 10:56
  • I would not care about the sorting algorithm unless it is a significant part of your code. I would use `Collections.sort()` and `Arrays.sort()` instead. – Arnaud Denoyelle Mar 06 '14 at 10:56
  • @PrR3 I get a warning in my IDE telling me that it's not applicable for the type int[], but only Integer[]. – Keir Simmons Mar 06 '14 at 10:58
  • @ArnaudDenoyelle Read my first paragraph, it's a programming exercise for myself, both for Java practice and algorithms practice. – Keir Simmons Mar 06 '14 at 10:58

1 Answers1

5

How do I fix this without using Integer[] arr = new Integer[]{..}?

You don't. This simply won't work.

I was under the impression that the compiler would box my int to Integer automatically?

It will, for individual int to Integer values. But there's no conversion from int[] to Integer[]... so combined with the lack of primitive support in Java generics, a method taking a T[] simply cannot take an int[].

There are third-party libraries which have conversion methods, but they will always create a new array, rather than creating a view onto the existing array. (It would be possible to create a List<Integer> which was a view onto the existing array, although obviously add operations would have to fail, as would set operations with a null value.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    How does the `java.utils.Arrays` package work when sorting an `int[]`? – Keir Simmons Mar 06 '14 at 11:00
  • 1
    @Keir: It has a separate method for it - it's as simple as that. I suspect each overload has a separate - possibly autogenerated - implementation. See the source for details of course. – Jon Skeet Mar 06 '14 at 11:09