logo

Arraycopy()-Methode des Java-Systems

Die arraycopy()-Methode der Java-Systemklasse gibt Teilsequenzkomponenten eines angegebenen Quellarrays zurück oder kopiert sie, beginnt an einer angegebenen Position (bezeichnet als „srcPos“) des Quellarrays (bezeichnet als „src“) bis zur angegebenen Position (bezeichnet als). „destPos“) des Zielarrays (bezeichnet als „dest“). Das Argument „Länge“ gibt die Anzahl der zu kopierenden Komponenten an.

Syntax

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

Parameter

src - Quellarray (Objekttyp)

np.wo

srcPos - Startposition im Quellarray (Integer-Typ)

Start - Zielarray (Objekttyp)

Zielpos - Startposition im Zielarray (Integer-Typ)

Länge - Anzahl der zu kopierenden Elemente (Integer-Typ)

Kehrt zurück

  1. Wenn dest oder src null ist, wird eine NullPointerException ausgelöst.
  2. Wenn die Argumente src und dest auf dasselbe Array-Objekt verweisen, beginnt das Kopieren der Komponenten mit srcPos. Zuerst werden die Komponenten in ein temporäres Array kopiert und dann an die Position des Ziel-Arrays kopiert.
  3. Wenn eines der Argumente vom Typ int einen negativen Wert hat, wird eine IndexOutOfBoundsException ausgelöst.
  4. Wenn sich die Argumente src und dest auf ein Objekt beziehen, das kein Array ist, wird eine ArrayStoreException ausgelöst

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

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