Java Custom Generic interview questions: receive arbitrary arrays for inversion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.