Parameterized method return type, parameterized Array

Source: Internet
Author: User

Sometimes, we want a method to return values of the corresponding type based on the parameter type. In addition, if we want to return the array parameters, we can see a short piece of useful code in think in Java today, share!

Two methods can be used for implementation: Class parameters and method parameters. I personally think the method parameters are good and flexible.

Package Oliver. java. test. array; <br/> Import Java. util. arrays; <br/> class classparameter <t >{< br/> Public T [] func (T [] Arg) {return ARG ;} <br/>}< br/> class methodparameter {<br/> Public static <t> T [] func (T [] Arg) {return ARG ;} <br/>}< br/> public class parameterizedarraytype {<br/> Public static void main (string [] ARGs) {<br/> // instantiate classparameter <br/> classparameter <integer> CP = new classparameter <integer> (); <br/> integer [] int1 = {1, 2, 3, 4, 5 }; <br/> // create an array using class parameters <br/> integer [] int2 = CP. func (int1); <br/> // create an Array Using method parameters <br/> integer [] int3 = methodparameter. func (int1); <br/> // print the result <br/> system. out. println (arrays. tostring (int2); <br/> system. out. println (arrays. tostring (int3); <br/>}< br/>}

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.