logo

Java System arraycopy() metode

Arraycopy()-metoden for Java System-klassen returnerer eller kopierer en undersekvenskomponenter af et specificeret kildearray, begynder ved en specificeret position (refereret som'srcPos') af kildearrayet (benævnt 'src') til den specificerede position (omtalt som 'destPos') for destinationsmatrix (benævnt 'dest'). Argumentet 'længde' er for antallet af komponenter, der skal kopieres.

Syntaks

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

Parametre

src - Kildearray (objekttype)

maskinsprog

srcPos - Startposition i kildearray (heltalstype)

Start - Destinationsarray (objekttype)

destpos - Startposition i destinationsmatrix (heltalstype)

længde - Antal elementer, der skal kopieres (heltalstype)

Vender tilbage

  1. Hvis dest eller src er null, kastes en NullPointerException.
  2. Hvis src- og dest-argumenter refererer til det samme array-objekt, vil kopiering af komponenter begynde med srcPos. Først vil komponenterne blive kopieret til et midlertidigt array og derefter til positionen for destPos for destinationsarrayet.
  3. Hvis et argument af typen int har negativ værdi, udløses der en IndexOutOfBoundsException.
  4. Hvis src og dest argumenter refererer til et objekt, der ikke er et array, kastes ArrayStoreException

Eksempel 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++)>

Eksempel 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++)>