Java中的System類是lang包的一部分,並且帶有許多不同的字段和方法,System.arraycopy()就是28個方法之一。它從特定索引值複製源數組並將其粘貼到另一個相同長度或不同長度的數組中,這並不重要,並將其複製到一定的長度,這也由我們決定。給定一個固定長度的數組。任務是在數組的特定索引處添加新元素。
--> java.lang Package --> System Class --> arraycopy() Method
用法:
public static void arraycopy(Object Source, int sourceStartIndex, Object Destination, int DestinationStartIndex, int size)
參數:
- Source:這是要從中複製元素的數組。
- sourceStartIndex:這是源開始位置。
- 目標:這是要複製元素的數組。
- DestinationStartIndex:這是目標起始位置。
- size:要複製的元素數量。
插圖
Input: arr[] = { 1, 2, 3, 4, 5 }, index = 2, element = 6 Output: arr[] = { 1, 2, 6, 3, 4, 5 } Input: arr[] = { 4, 5, 9, 8, 1 }, index = 3, element = 3 Output: arr[] = { 4, 5, 3)
方法:
- 獲取數組和索引 - p
- 創建一個新數組,其大小比原始數組的大小大一。
- 使用 System.arraycopy() 將原始數組中從起始位置到索引位置的元素複製到另一個數組。
- 在 p 處,將新元素添加到新數組中,該數組的大小比實際數組大 1
- 複製原始數組 p 中的元素,並使用 System.arraycopy() 將它們從索引 p+1 粘貼到新數組中直到末尾。
- 打印新數組
下麵是上述方法的實現。
Java
// Java Program to Illustrate arraycopy() Method
// of System class
// Importing required classes
import java.lang.System;
import java.util.Arrays;
import java.util.Scanner;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Original Array
int[] A = { 1, 2, 3, 4, 5, 6 };
System.out.print("Original Array : ");
System.out.print(Arrays.toString(A));
// size of the index
int n = A.length;
// Index at which the element be added
int p = 2;
// element that needs to be added
int x = 10;
// new Array of size +1
// of the original array
int[] B = new int[n + 1];
// Copy the elements from starting till index
// from original array to the other array
System.arraycopy(A, 0, B, 0, p);
// Copy the elements from p till
// end from original array
// and paste it into the new array on p+1 index
System.arraycopy(A, p, B, p + 1, n - p);
// putting the element x on the
// index p of the new array
B[p] = x;
// Printing the new Array
System.out.println();
System.out.print("New Array : "
+ Arrays.toString(B));
}
}
輸出
Original Array : [1, 2, 3, 4, 5, 6] New Array : [1, 2, 10, 3, 4, 5, 6]
相關用法
- Java System.arraycopy()用法及代碼示例
- Java System.out.println()用法及代碼示例
- Java System.exit()用法及代碼示例
- Java System.lineSeparator()用法及代碼示例
- Java System.identityHashCode()用法及代碼示例
- Java System.out.println用法及代碼示例
- Java System.out.println()和System.err.println()的區別用法及代碼示例
- Java System.out.print()和System.out.println()的區別用法及代碼示例
- Java System arraycopy()用法及代碼示例
- Java System clearProperty()用法及代碼示例
- Java System console()用法及代碼示例
- Java System currentTimeMillis()用法及代碼示例
- Java System exit()用法及代碼示例
- Java System gc()用法及代碼示例
- Java System getLogger()用法及代碼示例
- Java System getProperties()用法及代碼示例
- Java System getProperty()用法及代碼示例
- Java System identityHashCode()用法及代碼示例
- Java System inheritedChannel()用法及代碼示例
- Java System lineSeparator()用法及代碼示例
- Java System load()用法及代碼示例
- Java System mapLibraryName()用法及代碼示例
- Java System nanoTime()用法及代碼示例
- Java System setErr()用法及代碼示例
- Java System setIn()用法及代碼示例
注:本文由純淨天空篩選整理自ateevduggal大神的英文原創作品 System.arraycopy() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。