首頁  >  文章  >  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