You can only manipulate a type to invert without a generic type
The code is as follows:
Packagecom.swift.fanxing;Importorg.junit.Test; Public classRenyireverse {@Test Public voidTest () {intarr[]=New int[] {1,10,6,9,2}; Reverse (arr); } Public voidReverseint[] arr) { for(inti=0;i<arr.length/2;i++) { inttemp=Arr[i]; Arr[i]=arr[arr.length-1-i]; Arr[arr.length-1-i]=temp; } for(intX:arr) {System.out.println (x); } }}
Generic functionality can also receive variables or arrays of any type class, in addition to the collection iterator
Packagecom.swift.fanxing;Importorg.junit.Test; Public classRenyireverse {@Test Public voidTest () {Integer arr[]=NewInteger[] {1,10,6,9,2};//Note that you cannot use int instead of integerString arr2[]=NewString[] {"AA", "BB", "CC", "dd", "EE"}; Reverse (arr); Reverse (ARR2); } Public<T>voidreverse (t[] arr) { for(inti=0;i<arr.length/2;i++) {T temp=Arr[i]; Arr[i]=arr[arr.length-1-i]; Arr[arr.length-1-i]=temp; } for(T X:arr) {System.out.println (x); } }}
Non-static declarations before, statically declared after static, declarations on classes represent global, multi-generic declarations with <T,J,K>
Java Custom Generic interview questions: receive arbitrary arrays for inversion