java8中将数组进行分割_如何在Java中将一个数组复制到另一个数组

java8中将数组进行分割

Here you will learn how to copy one array to another in Java.

在这里,您将学习如何使用Java将一个数组复制到另一个数组。

There are mainly four different ways to copy all elements of one array into another array in Java.

在Java中,主要有四种方法可以将一个数组的所有元素复制到另一个数组中。

1. Manually 2. Arrays.copyOf() 3. System.arraycopy() 4. Object.clone()

1.手动 2. Arrays.copyOf() 3. System.arraycopy() 4. Object.clone()

Lets discuss each of them in brief.

让我们简单地讨论每个。

java8中将数组进行分割_如何在Java中将一个数组复制到另一个数组

如何在Java中将一个数组复制到另一个数组 (How to Copy One Array to Another in Java)

1.手动 (1. Manually)

In this method we manually copy elements one by one. It is not an efficient way. Below example shows how to do this.

在这种方法中,我们一一手动复制元素。 这不是一种有效的方法。 以下示例显示了如何执行此操作。

Example:

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com;
public class CopyArrayExample {
public static void main(String args[]){
int a[]={10,20,30,40,50};
int b[]=new int[a.length];
//copying one array to another
for(int i=0;i<a.length;++i){
b[i]=a[i];
}
//printing array
for(int i=0;i<b.length;++i){
System.out.print(b[i]+" ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com ;
public class CopyArrayExample {
public static void main ( String args [ ] ) {
int a [ ] = { 10 , 20 , 30 , 40 , 50 } ;
int b [ ] = new int [ a . length ] ;
//copying one array to another
for ( int i = 0 ; i < a . length ; ++ i ) {
b [ i ] = a [ i ] ;
}
//printing array
for ( int i = 0 ; i < b . length ; ++ i ) {
System . out . print ( b [ i ] + " " ) ;
}
}
}

2. Arrays.copyOf() (2. Arrays.copyOf())

We can directly copy one array to another by using Arrays.copyOf() method. It has following syntax.

我们可以使用Arrays.copyOf()方法将一个数组直接复制到另一个数组。 它具有以下语法。

1
public static int[] copyOf(int[] original,int newLength)
1
public static int [ ] copyOf ( int [ ] original , int newLength )

Example:

例:

3. System.arraycopy() (3. System.arraycopy())

It is another method that directly copies one array to another. It has following syntax.

这是将一个数组直接复制到另一个数组的另一种方法。 它具有以下语法。

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

Example:

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com;
public class CopyArrayExample {
public static void main(String args[]){
int a[]={10,20,30,40,50};
int b[]=new int[a.length];
//copying one array to another
System.arraycopy(a,0,b,0,a.length);
//printing array
for(int i=0;i<b.length;++i){
System.out.print(b[i]+" ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com ;
public class CopyArrayExample {
public static void main ( String args [ ] ) {
int a [ ] = { 10 , 20 , 30 , 40 , 50 } ;
int b [ ] = new int [ a . length ] ;
//copying one array to another
System . arraycopy ( a , 0 , b , 0 , a . length ) ;
//printing array
for ( int i = 0 ; i < b . length ; ++ i ) {
System . out . print ( b [ i ] + " " ) ;
}
}
}

4. Object.clone() (4. Object.clone())

We can also use clone() method of Object class to make a copy of an array.

我们还可以使用Object类的clone()方法制作数组的副本。

Example:

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com;
public class CopyArrayExample {
public static void main(String args[]){
int a[]={10,20,30,40,50};
int b[]=new int[a.length];
//copying one array to another
b=a.clone();
//printing array
for(int i=0;i<b.length;++i){
System.out.print(b[i]+" ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com ;
public class CopyArrayExample {
public static void main ( String args [ ] ) {
int a [ ] = { 10 , 20 , 30 , 40 , 50 } ;
int b [ ] = new int [ a . length ] ;
//copying one array to another
b = a . clone ( ) ;
//printing array
for ( int i = 0 ; i < b . length ; ++ i ) {
System . out . print ( b [ i ] + " " ) ;
}
}
}

Comment below if you have any doubt related to above tutorial or you know about any other way to copy one array to another in Java.

如果您对以上教程有任何疑问,或者您知道用Java将一个数组复制到另一个数组的任何其他方法,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/05/copy-one-array-another-java.html

java8中将数组进行分割