MyTetra Share
Делитесь знаниями!
Копирование массивов / java.lang.System.arraycopy()
Время создания: 25.09.2017 10:56
Текстовые метки: code
Раздел: Java - TypesData - Контейнеры - Array
Запись: xintrea/mytetra_db_mcold/master/base/1506326171sqbwbti2yq/text.html на raw.githubusercontent.com

Description

The java.lang.System.arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest.The number of components copied is equal to the lengthargument.

The components at positions srcPos through srcPos + length - 1 in the source array are copied into positions destPos through destPos + length - 1, respectively, of the destination array.

Declaration

Following is the declaration for java.lang.System.arraycopy() method

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

Parameters

  • src − This is the source array.
  • srcPos − This is the starting position in the source array.
  • dest − This is the destination array.
  • destPos − This is the starting position in the destination data.
  • length − This is the number of array elements to be copied.

Return Value

This method does not return any value.

Exception

  • IndexOutOfBoundsException − if copying would cause access of data outside array bounds.
  • ArrayStoreException − if an element in the src array could not be stored into the dest array because of a type mismatch.
  • NullPointerException − if either src or dest is null.

Example

The following example shows the usage of java.lang.System.arraycopy() method.

package com.tutorialspoint;

import java.lang.*;

public class SystemDemo {

   public static void main(String[] args) {

      int arr1[] = { 0, 1, 2, 3, 4, 5 };
      int arr2[] = { 5, 10, 20, 30, 40, 50 };
    
      // copies an array from the specified source array
      System.arraycopy(arr1, 0, arr2, 0, 1);
      System.out.print("array2 = ");
      System.out.print(arr2[0] + " ");
      System.out.print(arr2[1] + " ");
      System.out.print(arr2[2] + " ");
      System.out.print(arr2[3] + " ");
      System.out.print(arr2[4] + " ");
   }
}

Let us compile and run the above program, this will produce the following result −

array2 = 0 10 20 30 40 
 
MyTetra Share v.0.65
Яндекс индекс цитирования