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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。