Co to jest metoda Java System.arraycopy()?
Deklaracja metody Java System.arraycopy().
Deklaracja metody dla metody java.lang.System.arraycopy() w klasie java.lang jest następująca:
public static void arraycopy(Object src, int srcIndex, Object dest, int destIndex, int len)
Parametry
Poniżej przedstawiono parametry metody arraycopy :-
src : To jest tablica źródłowa.
-
srcIndex : Jest to początkowy indeks tablicy źródłowej.
-
dest : Jest to tablica docelowa.
-
destIndex : Jest to początkowy indeks tablicy docelowej.
-
len : Jest to liczba elementów, które należy skopiować z tablicy źródłowej do tablicy docelowej.
Funkcjonalność
Metoda arraycopy kopiuje dane z src , zaczynając od srcIndex do elementów srcIndex +( len - 1) do tablicy dest w destIndex do destIndex + ( len - 1).Wartość zwracana
Metoda arraycopy ma typ zwrotu void , co oznacza, że nic nie zwraca.Przykład
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] + " ");
}
}
Wyjście
len : 2
srcIndex : 2
src : poniedziałek wtorek środa czwartek piątek sobota niedziela
destIndex : 3
cel : styczeń luty marzec kwiecień maj czerwiec lipiec sierpień
ostateczna tablica docelowa : styczeń luty marzec środa czwartek czerwiec lipiec sierpień
Przykład zmiany parametrów
// Example of changing parameters
srcIndex = 4;
destIndex = 5;
len = 1;
Wyjście
len : 1
srcIndex : 4
src : poniedziałek wtorek środa czwartek piątek sobota niedziela
destIndex : 5
cel : styczeń luty marzec kwiecień maj czerwiec lipiec sierpień
ostateczna tablica docelowa : styczeń luty marzec kwiecień maj piątek lipiec sierpień
GO TO FULL VERSION