Making BubbleSort Accept Generics

Posted 06 December 2012 - 01:10 PM

Hi. this is my first post so please forgive me for errors.

I have made a Bubble Sort for ints.
my next step is to make my Bubble Sort
more sophisticated by making it work
for a generic array. I have to write a
test program for my method using arrays
with these three data types: Double, Character, and String.

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 01:24 PM

You can't have generic array of basic datatype in Java
If it is an array of Object like (Integer, Character, Doubkle, Float, ...) you can simply declare the tyupe of the array as Comparable in the method parameter

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 02:15 PM

As proof of concept. Notice how comparing Apples and Oranges crashes at runtime, but it is fine to store the two in the array. Sorting relies on comparisons, so the problem should be clear. Whereas if you use methods to enforce that the elements are mutually Comparable, you won't run into the problem of worrying about comparisons causing a crash (because of type) during runtime.