首页  >  文章  >  Java  >  Java 可变参数

Java 可变参数

王林
王林原创
2024-08-30 15:51:05723浏览

Java varargs 是一个 Java 编程语言概念,它简化了创建实际需要的接受一些可变数量参数的方法的概念。此功能/概念称为 varargs,varargs 是可变长度参数的简称。这里,采用可变数量参数的方法是可变参数方法。在JDK 5版本之前,可以使用varargs(可变长度参数)以两种方式进行处理。一种方法是使用重载方法,第二种方法是将参数放入数组中,这样将数组传递给方法就完成了。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

public static void fun(int …a1)
{
//method body1
}

说明:这里,fun() 函数是使用参数“int …a1”创建的。然后方法体就会被输入到执行的时候使用。在特定方法中应该只使用一个变量 arg-arguments。 vararg – 可变长度参数必须是最后一个参数;如果没有,可能会发生编译。

可变参数在 Java 中如何工作?

Java 编程语言的 varargs(可变长度参数)将有助于简化实际采用可变数量参数的方法的创建。采用可变数量 args 参数的 Java 方法称为 varargs 方法。它是JDK5版本的。在 JDK 5 版本之前,可变长度参数将以两种方式处理。第一种方法是借助重载方法,另一种方法是将args-arguments放入特定的数组中,然后将该数组传递给特定的方法。大多数时候,两者都可能容易出错,并且需要越来越多的代码。可变参数概念的工作原理是允许方法实际接受多个参数或不接受多个参数。

“...”语法将通过告诉编译器 varargs 与这些 args 参数一起使用来工作,它们应该存储在 a1 引用的特定数组中。

vararg-variable argument/Variable-Length Arguments 方法可能会重载,但重载会导致特定的歧义。从之前的 JDK 5 版本开始,varargs - 可变长度参数可以或也可以以两种类型的方式处理。第一种方法是重载,第二种方法是使用数组 arg-argument。方法中只有一种类型的变量参数。 varargs(变量参数)将是或必须是最后一个参数。

也有错误的可变参数。一种是仅在一个方法中指定两个可变参数。第二种是指定方法的第一个参数而不是最后一个参数。

该方法可以有一个变量的长度参数和一些其他不同的参数,但必须确保只存在一个可变参数,并且它应该写在方法声明的最后一部分的参数中。

实现 Java 可变参数的示例

下面是一些提到的例子:

示例#1

这是使用 Java 编程语言实现的 varargs。首先,创建一个类“Test11”,然后使用 varargs 语法创建 fun1()。然后使用system.out.print()函数配合长度计算语法“a1.length”,它只不过是计算数组的元素个数。然后创建 FOR-EACH 循环来了解特定数组的每个元素。然后 system.out.print() 函数仅用于打印每个数组的元素。然后 system.out.println() 仅用于命令提示符输出中的换行符。然后创建“Public static void main(String args[])”来提及特定函数“fun1()”内部的数组元素。三种不同类型的数组元素,具有不同的长度。查看下面输出部分的输出。

代码:

class Test11
{
static void fun1(int ...a1)
{
System.out.println("The Number of the arguments metioned: " + a1.length);
for (int i1: a1)
System.out.print(i1 + " ");
System.out.println();
}
public static void main(String args[])
{
fun1(1000);
fun1(11, 12, 13, 14);
fun1();
}
}

输出:

Java 可变参数

示例#2

这是一个 Java 程序的示例,其中仅使用普通参数实现了 varargs 概念。首先,使用新函数“fun21”创建一个类“Test21”,该函数具有两种不同类型的参数/参数。在 fun21() 函数内部,system.out.print() 创建了 2 次。第一次创建是打印str2值,然后第二次创建是打印数组的长度。然后创建 for-each 循环来提取数组中不同类型和不同数量元素的元素。在 FOR 循环的帮助下,只会打印数字参数/值,而字符串元素将以不同的方式打印。

代码:

class Test21
{
static void fun21(String str2, int ...a2)
{
System.out.println("String: " + str2);
System.out.println("Number of arguments is: "+ a2.length);
for (int i2: a2)
System.out.print(i2 + " ");
System.out.println();
}
public static void main(String args[])
{
fun21("PavanKumarSakeCEOofProfitLoops", 1000, 2000);
fun21("ProfitLoops", 21, 22, 23, 24, 25);
fun21("fortheWorld");
}
}

Output:

Java 可变参数

Example #3

This is an example of implementing a varargs program in Java code. Here at first, Test 21 class is created, then the display () static function is created. Then inside of it, system.out.println() is used to print the string element. Then the for-each concept is used to print all the elements of the array. Then the parenthesis closing is done for the function display(). Then “public static void main(String_args[])” is used for entering the code, which actually returns the output. Then the display() is used with different types of array elements with different lengths.

Code:

class Test21{
static void display(String... values1){
System.out.println("Here Now the display method is invoked ");
for(String s1:values1){
System.out.println(s1);
}
}
public static void main(String args[]){
display();
display("Howdyhello");
display("my","name","is","varargs-pavan");
display("Pavan Kumar Sake is the future billionaire. Soon You will see.. :-)");
}
}

Output:

Java 可变参数

Conclusion

I hope you learned the actual definition of Java varargs (Variable Length Arguments) along with their syntax and explanation, How the varargs works in the Java Programming Language, and the various examples of implementing the varargs concept to understand better.

以上是Java 可变参数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java ZoneOffset下一篇:Java LocalDate