A new type of generic parameter transfer method after "Java" JDK1.5 Object...args

Source: Internet
Author: User

Object...args is a new type of parameter transmission after JDK1.5, which has become more and more popular. It is useful in situations where the number of unknown parameter variables or a method needs to deal with different number of variables and does not want to rewrite the method. Because it does not need to define an array as it used to, or even define a dynamic array of ArrayList with the rewrite of the method to complete the transfer of the variable. Greatly improves the reusability of the program. Perhaps this passage is a bit abstract, or to give a simple and understandable example to explain it!

I want to achieve the following effect, when a method receives 5 integer variable int, output these 5 shaping variables, and sum, if this method receives 11 integer variable int, do the same thing. If you only receive 2 shaping variables, just output "you're coming up with two parameters!" ”。


This is for most of the students who have finished the Java course will use the same name method in the case of different variables of the rewrite to solve the problem, but if I asked to receive X variables to do the output and sum up this thing? This x is from 1 to 200000, then some people will propose to use the transfer ArrayList dynamic array to the same name method to solve the problem. In fact, with JDK1.5 after the new type of generic parameter transfer method Object...args you do not need to write so complex, write so much code.

The following code implements the same effect as the run.

Class TestClass {public void testoverloading (int i, int j) {System.out.println ("You came up with two parameters! ");} public void testoverloading (int ... args) {System.out.println ("the number of arguments passed over is" + Args.length + "each"); System.out.print ("respectively:"); int total = 0;for (int i:args) {System.out.print (i + ""); System.out.println (); System.out.println ("Sum of:" + total);}} public class Newvariable {public static void main (string[] args) {//Find public void testoverloading (int ... args) {}new testcl (). testoverloading (1, 2, 3, 4, 5); System.out.println ("======================");//Find public void testoverloading (int ... args) {}new TestClass (). Testoverloading (1, 2, 3, 4, 5, 6, 1, 213, 123, 24, 1); System.out.println ("======================");//Because there is a public void testoverloading (int i, int j) {}, so use this method//Ignore public void testoverloading (int ... args) {}new TestClass (). testoverloading (1, 2);}}

As can be seen, in the TestClass testoverloading () method, the face of different parameters have different solutions, this is called to be rewritten several times. Call this method, if passed over the argument is not two, then call the so-called generic method public void testoverloading (int ... args) {}, this thing for no corresponding parameters of the solution is applicable, so called generics! Formal parameters Int...args and int[] args or int args[] No difference, is the shape array args[], you can follow a normal shape array operation it, but in order to implement generics, you have to write Int...args. In this method also used JDK1.5 after the new traversal way traversal, specifically see "Java" with JDK1.5 after the new array traversal method traversal HashMap, HashMap should not store multivariate group "(Click Open link), no longer repeat.

A new type of generic parameter transfer method after "Java" JDK1.5 Object...args

Related Article

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.