Good Night :
Before Java SE 5.0 every Java method had a fixed number of parameters.
However, it is now possible to provide methods that can be called with a variable number of parameters.
(These are sometimes called “varargs” methods.)
You can define your own methods with variable parameters, and you can specify any type for the parameters, even a primitive type.
Here is a simple example: a function that computes the maximum of a variable number of values
لمحة عربية سريعة
قبل ان تصدر شركة Sun الحزمة السادسة من JDK اي عندما كانت النسخة المستخدمة هي JDK 5 , كانت لكل الدوال (التوابع ) عدد محدد من المتحولات او نسطيع ان نقول عدد ثابت فقط.
ولكن من النسخة السادسة JDK 6 قامت الشركة بالسماح للتوابع ذات العدد المتغير من المتحولات وتسمى VARARGS METHODS . والتي يمكن لنا ان نقوم بانشاء طريقة تحوي على عدد متغير من المتحولات .
مثال بسيط تابع يقوم بحساب اكبر رقم بين عدد غير محدد من الارقام .
كانت هذه العملية في الاصدارات السابقة نوعا ما غير محببة اما الان فنكتب تابع واحد بسيط كما يلي .
testClassOfVarPara.java
/**
*
* @author Eadoking
* @website http://eadoking.com
* @email eadoking@yahoo.com
* @email info@eadoking.com
* @mobile +963 932728084
*/
public class testClassOfVarPara {
// integer maximum method with variable number of parameters
public static int maximumVarPara(int... numbers) {
int max = Integer.MIN_VALUE;
for (int num : numbers) {
if (num > max) {
max = num;
}
}
return max;
}
// double maximum method with variavle number of parameters
public static double maximumVarPara(double... numbers) {
double max = Double.MIN_VALUE;
for (double num : numbers) {
if (num > max) {
max = num;
}
}
return max;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int inrArr[] = {10, 20, 50, 63, 2, 4, 9, 5};
System.out.println("The maximum of (int arr) is : " + maximumVarPara(inrArr));
double doubleArr[] = {93.2, 23.5, 263.74, .63, 0.3, 90.5};
System.out.println("The maximum of (double arr) is : " + maximumVarPara(doubleArr));
System.out.println("You can try any number of variable");
System.out.println("You can use more complex not only int");
System.out.println("You can use also objects");
}
}
For any question , just post it in comments and i’ll explain it
لاى استفسار او اي غموض غير واضح الرجاء وضع تعليق بالجزء الغير واضح وساقوم بالتوضيح
If u like this post you may like
- » How to connect C# (C SHARP ) programs to MySQL Database with source code كيفية ربط برنامج بلغة سي شارب مع قاعدة بيانات MySQL
- » كيفية تحميل وتثبيت واعداد نظام قواعد البيانات الشهير MySQL Server 5.1 باحدث اصدار لها
- » كيفية إصلاح سجل الإقلاع الأساسي Master Boot Record في مختلف انظمة الويندوز او كما يسمى ملف ال MBR
- » Camtasia Studio 7.0.0 build 1426 march 2010 full with serials and direct links احدث اصدار من احدث واقوى برنامج لتسجيل سطح المكتب كامل وروابط مباشرة
- » التحكم بخدمات الاوراكل وجعل عملها يدويا وكيفية تسريع أقلاع الجهاز مع تشغيل isqlPlus



Twitter
Facebook
RSS
Delicious
LinkedIn