int[] arr1={1, 2, 3, 4, 5, 6}; //first array
int[] arr2={7, 8, 9, 0}; //second array
int[] arr3={1, 2, 3, 4, 5, 6, 7, 8, 9, 0} //resultant array
There are following ways to merge two arrays:
1.Java arraycopy() method
2.Without using arraycopy() method
3.Java Collections
4.Java Stream API
1.Java arraycopy() method
Java arraycopy() is the method of System class which belongs to java.lang package.
It copies an array from the specified source array to the specified position of the destination array.
The number of elements copied is equal to the length argument.
Syntax:
public static void arraycopy(Object source, int source_position, Object destination, int destination_position, int length)
Parameters
- source: It is a source array.
- source_position: Starting point in the source array.
- destination: It is a destination array.
- destination_position: Starting position in the destination array.
- length: The number of array elements to be copied
NullPointerException
It throws NullPointerException if the source or destination array is null. It also throws ArrayIndexOutOfBoundsException if:
- source_position or destination_position or length is negative.
- source_position+length is greater than the length of the source array, or destination_position+length is - greater than the length of the destination array.
public class A{
public static void main(String[] args) {
int[] firstArray = {23,45,12,78,4,90,1}; //source array
int[] secondArray = {77,11,45,88,32,56,3}; //destination array
int fal = firstArray.length; //determines length of firstArray
int sal = secondArray.length; //determines length of secondArray
int[] result = new int[fal + sal]; //resultant array of size first array and second array
System.arraycopy(firstArray, 0, result, 0, fal);
System.arraycopy(secondArray, 0, result, fal, sal);
System.out.println(Arrays.toString(result)); //prints the resultant array
}
}
[23, 45, 12, 78, 4, 90, 1, 77, 11, 45, 88, 32, 56, 3]
according to the specified positions and length.
public class A{
public static void main(String[] args) {
int source_arr [] = { 11,22,33,44,55,98,76,54,60};
int dest_arr[] = {66,77,88,99,22,67,21,90,80,70};
int sourcePos = 2;
int destPos = 4;
int len = 3;
//invoking arraycopy() method
System.arraycopy(source_arr, sourcePos, dest_arr,destPos, len);
// Print elements of destination after
System.out.print("Resultant array : ");
for (int i = 0; i < dest_arr.length; i++)
System.out.print(dest_arr[i] + " ");
}
}
Resultant array : 66 77 88 99 33 44 55 90 80 70
2.Without using arraycopy() method
Manually copy the each element of both arrays to mergedArray and convert that array into String by using toString() method of Array class.
public class A{
public static void main(String[] args) {
int[] firstArray = {56,78,90,32,67,12}; //initialized array
int[] secondArray = {11,14,9,5,2,23,15};
int length = firstArray.length + secondArray.length; //add the length of firstArray into secondArray
int[] mergedArray = new int[length]; //resultant array
int pos = 0;
for (int element : firstArray) //copying elements of secondArray using for-each loop
{
mergedArray[pos] = element;
pos++; //increases position by 1
}
for (int element : secondArray) //copying elements of firstArray using for-each loop
{
mergedArray[pos] = element;
pos++;
}
System.out.println(Arrays.toString(mergedArray)); //prints the resultant array
}
}
[56, 78, 90, 32, 67, 12, 11, 14, 9, 5, 2, 23, 15]
3.Java Collections
Using the Arrays.asList() method.
Now we have created the list view of str2 and added all the elements of str2 into the list.
Again perform conversion from list to array and store the resultant array into str3 variable.
public class A{
public static void main(String[] args) {
String str1[] = { "A", "E", "I" }; //source array
String str2[] = { "O", "U" }; //destination array
List list = new ArrayList(Arrays.asList(str1)); //returns a list view of an array
//returns a list view of str2 and adds all elements of str2 into list
list.addAll(Arrays.asList(str2));
Object[] str3 = list.toArray(); //converting list to array
System.out.println(Arrays.toString(str3)); //prints the resultant array
}
}
[A, E, I, O, U]
4.Java Stream API
Stream.of() method
The Stream.of() method of Stream interface returns a sequential ordered stream whose elements are the values.Syntax
static Stream of(T…values)
Explain:Where MT is the type of stream elements. The method accepts values (elements of the new stream).
flatMap() method
The flatMap() method is the method of Stream interface. It returns a stream consisting of the result.
Syntax
Stream flatMap(Function<? Super T, ? extends Stream<? Extends R>> mapper)
Explain:Where R is the element type of new stream. The method accepts a mapper (a function to apply to each element which produces a stream of new values) as a parameter.文章来源:https://www.toymoban.com/news/detail-513345.html
toArray() method
The toArray() method of Stream interface returns an array containing the elements of the stream.
Syntax
Object[] toArray()文章来源地址https://www.toymoban.com/news/detail-513345.html
public class A{
// function to merge two arrays
public static <T> Object[] mergeArray(T[] arr1, T[] arr2)
{
return Stream.of(arr1, arr2).flatMap(Stream::of).toArray();
}
public static void main (String[] args)
{
Integer[] firstArray = new Integer[]{13,12,11,6,9,3}; //source array
Integer[] secondArray = new Integer[]{78,34,56,67,2,11,7}; //destination array
Object[] mergedArray = mergeArray(firstArray,secondArray); //merged array
System.out.println("Merged array: "+ Arrays.toString(mergedArray));
}
}
Merged array: [13, 12, 11, 6, 9, 3, 78, 34, 56, 67, 2, 11, 7]
到了这里,关于Java中合并两个数组的4种方法(How to Merge Two Arrays in Java)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!