Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The function of Java variable parameters and how to write the code

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the role of Java variable parameters and how to write code". In daily operation, I believe many people have doubts about the role of Java variable parameters and how to write code. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to answer your doubts about "the role of Java variable parameters and how to write code"! Next, please follow the small series to learn together!

Java variable parameters:

In the course of writing methods, you may encounter a case where a method has an indefinite number of arguments. We usually use method overloading to solve problems:

1 //method overload, solve the problem of uncertain number of parameters 2 public void method();3 public void method(int i);4 public void method(int i, int j);5 public void method(int i, int j, int k);

However, when the number of parameters is too many, this is too cumbersome, so we can use the indefinite parameter (variable parameter) way:

public void test(int... args);

It's like an array, but in a different way.

/* Since JDK 1.5, Java supports passing variable parameters of the same type to a method.

*

* In the method declaration, add an ellipsis (…) after specifying the parameter type

* Only one variable parameter can be specified in a method, and it must be the last parameter of the method. Any generic arguments must be declared before it.

examples

1

public class Demo04 { public static void main(String[] args) { Demo04 demo04 = new Demo04(); demo04.test(1,2,3,4,5,6);//Call the created method } public void test(int... i){//multiple i's, avoid cumbersome, can be written int... i, i.e. become multiple i System.out.println(i[0]); System.out.println(i[1]); System.out.println(i[2]); System.out.println(i[3]); System.out.println(i[4]); }}

2

package javamethod;//--------Examples of variable parameters public class Demo05 { public static void main(String[] args) { //call method with variable parameters printMax(0.); } //define a method for outputting maximum values public static void printMax(double... numbers){ if (numbers.length==0){ System.out.println("Don't do it, brother. "); return; } double result=numbers[0]; //Sort for (int i=1;iresult){ result=numbers[i]; } } System.out.println("Baby, maximum number is"+result); }} At this point, the study of "the role of Java variable parameters and how to write code" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report