Java System.arraycopy() طریقہ کیا ہے؟
Java System.arraycopy() طریقہ کا اعلان
java.lang.System.arraycopy() طریقہ java.lang کلاس میں طریقہ کا اعلان درج ذیل ہے:
public static void arraycopy(Object src, int srcIndex, Object dest, int destIndex, int len)
پیرامیٹرز
arraycopy طریقہ کے پیرامیٹرز درج ذیل ہیں :-
src : یہ سورس سرنی ہے۔
-
srcIndex : یہ سورس ارے کا ابتدائی انڈیکس ہے۔
-
dest : یہ منزل کی صف ہے۔
-
destIndex : یہ منزل کی صف کا ابتدائی اشاریہ ہے۔
-
len : یہ عناصر کی تعداد ہے جنہیں ماخذ سرنی سے منزل کی صف میں کاپی کرنے کی ضرورت ہے۔
فعالیت
arraycopy کا طریقہ src سے ڈیٹا کاپی کرتا ہے ، srcIndex سے شروع ہو کر srcIndex +( len - 1) عناصر تک، destIndex پر destIndex + ( len - 1) تک ڈیسٹ ارے تک۔واپسی کی قیمت
arraycopy طریقہ میں باطل واپسی کی قسم ہے جس کا مطلب ہے کہ یہ کچھ بھی واپس نہیں کرتا ہے۔مثال
public class Example {
public static void main(String[] args) {
String[] src = { "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
String[] dest = { "January", "February", "March", "April", "May", "June", "July", "August"};
int srcIndex = 2;
int destIndex = 3;
int len = 2;
//print number of elements that need to be copied
//from the source to the destination array
System.out.println("len : " + len);
//print source index
System.out.println("srcIndex : " + srcIndex);
//print elements of the source array
System.out.print("src : ");
for (int i = 0; i < src.length; i++)
System.out.print(src[i] + " ");
System.out.println("");
//print destination index
System.out.println("destIndex : " + destIndex);
//print elements of the destination array
System.out.print("dest : ");
for (int i = 0; i < dest.length; i++)
System.out.print(dest[i] + " ");
System.out.println("");
// Use of arraycopy() method
System.arraycopy(src, srcIndex, dest, destIndex, len);
// this method copies the 'len' no of elements
// from the src array to the dest array using the srcIndex
// and destIndex as reference points in both the arrays
// Print elements of destination after
System.out.print("final destination array : ");
for (int i = 0; i < dest.length; i++)
System.out.print(dest[i] + " ");
}
}
آؤٹ پٹ
len : 2 srcIndex : 2 src : پیر منگل بدھ جمعرات جمعہ ہفتہ اتوار ڈیسٹ انڈیکس : 3 dest : جنوری فروری مارچ اپریل مئی جون جولائی اگست آخری منزل کی صف : جنوری فروری مارچ بدھ جمعرات جون جولائی اگست
پیرامیٹرز کو تبدیل کرنے کی مثال
// Example of changing parameters
srcIndex = 4;
destIndex = 5;
len = 1;
آؤٹ پٹ
len : 1 srcIndex : 4 src : پیر منگل بدھ جمعرات جمعہ ہفتہ اتوار destIndex : 5 dest : جنوری فروری مارچ اپریل مئی جون جولائی اگست آخری منزل کی صف : جنوری فروری مارچ اپریل مئی جمعہ جولائی اگست
GO TO FULL VERSION