logo

שיטת Java System arraycopy()

שיטת arraycopy() של מחלקת Java System מחזירה או מעתיקה רכיבי רצף של מערך מקור שצוין, מתחילה במיקום מוגדר (המכונה'srcPos') של מערך המקור (המכונה 'src') למיקום שצוין (המכונה 'destPos') של מערך היעד (המכונה 'dest'). הארגומנט 'אורך' הוא עבור מספר הרכיבים שיש להעתיק.

תחביר

 public static void arraycopy (Object src, int srcPos, Object dest, int destPos, int length) 

פרמטרים

src - מערך מקור (סוג אובייקט)

tcp לעומת udp

srcPos - מיקום התחלה במערך מקור (סוג מספר שלם)

הַתחָלָה - מערך יעדים (סוג אובייקט)

destpos - מיקום התחלה במערך היעד (סוג מספר שלם)

אורך - מספר האלמנטים להעתקה (סוג מספר שלם)

החזרות

  1. אם dest או src הם null אז נזרק NullPointerException.
  2. אם ארגומנטים src ו-dest מתייחסים לאותו אובייקט מערך אז העתקה של רכיבים תתחיל ב-srcPos. ראשית הרכיבים יועתקו מערך זמני ולאחר מכן למיקום של destPos של מערך היעד.
  3. אם לארגומנטים מסוג int יש ערך שלילי אז נזרק IndexOutOfBoundsException.
  4. אם ארגומנטים src ו-dest מתייחסים לאובייקט שאינו מערך אז ArrayStoreException נזרק

דוגמה 1

 public class SystemArraycopyExample1 { public static void main(String[] args) { int a[]= {1,2,3,4,3,2,1}; //source array int b[]= {5,6,7,8,7,6,5}; //destination array int src[],srcPos,dest[],destPos,length; src=a; srcPos=1; dest=b; destPos=2; length=4; System.out.print(&apos;Source array:&apos;); for(int i=0;i<src.length;i++) {system.out.print(a[i]);} system.out.println(); system.out.print('destination array:'); for(int i="0;i&lt;src.length;i++)" {system.out.print(b[i]);} system.out.println('source position:'+srcpos); system.out.println('destination position:'+destpos); system.out.println('length:'+length); system.arraycopy(src, srcpos, dest, destpos, length); use of arraycopy() method array after arraycopy()'); <b.length;i++) { system.out.print(b[i]); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Source array:1234321 Destination array:5678765 Source Position:1 Destination Position:2 Length:4 Destination array after use of arraycopy() 5623435 </pre> <h2>Example 2</h2> <pre> public class SystemArraycopyExample2 { public static void main(String[] args) { String a[]= {&apos;A&apos;,&apos;B&apos;,&apos;B&apos;,&apos;H&apos;,&apos;A&apos;,&apos;M&apos;}; //source array String b[]= {&apos;S&apos;,&apos;H&apos;,&apos;U&apos;,&apos;N&apos;,&apos;U&apos;,&apos;L&apos;,&apos;L&apos;,&apos;J&apos;,&apos;A&apos;,&apos;D&apos;,&apos;O&apos;,&apos;N&apos;}; //destination array String src[],dest[]; int srcPos,destPos,length; src=a; srcPos=2; dest=b; destPos=3; length=4; System.out.print(&apos;Source array:&apos;); for(int i=0;i<src.length;i++) {system.out.print(a[i]);} system.out.println(); system.out.print('destination array:'); for(int i="0;i&lt;src.length;i++)" {system.out.print(b[i]);} system.out.println('source position:'+srcpos); system.out.println('destination position:'+destpos); system.out.println('length:'+length); system.arraycopy(src, srcpos, dest, destpos, length); use of arraycopy() method array after arraycopy()'); <b.length;i++) { system.out.print(b[i]); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Source array:ABBHAM Destination array:SHUNUL Source Position:2 Destination Position:3 Length:4 Destination array after use of arraycopy() SHUBHAMJADON </pre> <br></src.length;i++)></pre></src.length;i++)>

דוגמה 2

 public class SystemArraycopyExample2 { public static void main(String[] args) { String a[]= {&apos;A&apos;,&apos;B&apos;,&apos;B&apos;,&apos;H&apos;,&apos;A&apos;,&apos;M&apos;}; //source array String b[]= {&apos;S&apos;,&apos;H&apos;,&apos;U&apos;,&apos;N&apos;,&apos;U&apos;,&apos;L&apos;,&apos;L&apos;,&apos;J&apos;,&apos;A&apos;,&apos;D&apos;,&apos;O&apos;,&apos;N&apos;}; //destination array String src[],dest[]; int srcPos,destPos,length; src=a; srcPos=2; dest=b; destPos=3; length=4; System.out.print(&apos;Source array:&apos;); for(int i=0;i<src.length;i++) {system.out.print(a[i]);} system.out.println(); system.out.print(\'destination array:\'); for(int i="0;i&lt;src.length;i++)" {system.out.print(b[i]);} system.out.println(\'source position:\'+srcpos); system.out.println(\'destination position:\'+destpos); system.out.println(\'length:\'+length); system.arraycopy(src, srcpos, dest, destpos, length); use of arraycopy() method array after arraycopy()\'); <b.length;i++) { system.out.print(b[i]); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Source array:ABBHAM Destination array:SHUNUL Source Position:2 Destination Position:3 Length:4 Destination array after use of arraycopy() SHUBHAMJADON </pre> <br></src.length;i++)>