Integer/ int autoboxing query with generic functions in Java -


okay, trying write generic sorting package in java (i'm taking algorithms course , see practice, both in algorithmic sense , in java i'm new language). anyway, figure best way create sort class multiple sorting methods within, such insertionsort, mergesort etc. if feel there's better way of doing this, please let me know comment open suggestions writing cleaner , more efficient code.

as question:

i have backbone structure down , want try coding insertionsort method first. however, have tried pass integer array getting problems. firstly, generics understand have use <integer> opposed <int>, reason if create array such int[] arr = new int[]{..} , pass in generic not work. how fix without using integer[] arr = new integer[]{..}? under impression compiler box int integer automatically?

my code:

public class sort<t> {     public t[] insertionsort(t[] array) {         // 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);     } } 

how fix without using integer[] arr = new integer[]{..}?

you don't. won't work.

i under impression compiler box int integer automatically?

it will, individual int integer values. there's no conversion int[] integer[]... combined lack of primitive support in java generics, method taking t[] cannot take int[].

there third-party libraries have conversion methods, create new array, rather creating view onto existing array. (it possible create list<integer> view onto existing array, although add operations have fail, set operations null value.)


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -