First of all, java arrays have fixed memory and size, so to concat or merge array two arrays you array.
have to create a new one with the size of arrayOne.length + arrayTwo.length add them to the new
have to create a new one with the size of arrayOne.length + arrayTwo.length add them to the new
Here some different solutions that can concat two different arrays into One Array.
Merging two arrays or multiple arrays using a custom function. It takes multiple arrays as an argument and merges or concat into single, this function is only applicable for the small array.
import java.util.Arrays; public class MergeArray { public static int[] mergerAarry(int[]... arrays) { int mergeArray[] = null; int lenghtcount = 0; for (int[] arr : arrays) { lenghtcount += arr.length; } mergeArray = new int[lenghtcount]; int indexcount = 0; for (int arr[] : arrays) { for (int insertarr : arr) { mergeArray[indexcount] = insertarr; ++indexcount; } } return mergeArray; } public static void main(String[] args) { int a[] = new int[] { 56, 5, 89 }; int b[] = { 78, 9, 30 }; int main[] = mergerAarry(a, b); System.out.println(Arrays.toString(main)); } }
OUTPUT: [56, 5, 89, 78, 9, 30]
Using ArrayList you can merge or concat to multiple Arrays, but using ArrayList you have to convert Primitive(int) to Wrapper class(Integer) or you can add directly Wrapper class(Integer) in ArrayList
import java.util.ArrayList; import java.util.Arrays; public class MergeArray { public static Integer[] toInteger(int[] array) { Integer[] output = new Integer[array.length]; for (int i = 0; i < array.length; i++) { output[i] = Integer.valueOf(array[i]); } return output; } public static void main(String[] args) { int first[] = new int[] { 56, 5, 89 }; Integer second[] = { 78, 9, 30 }; ArrayList<Integer> both = new ArrayList<Integer>(Arrays.asList(toInteger(first))); both.addAll(Arrays.asList(second)); both.toArray(new Integer[both.size()]); System.out.println(both); } }
OUTPUT: [56, 5, 89, 78, 9, 30]
Using stream you can merge or concat two arrays into a single array while using stream you need to make Stream as a generic.
import java.util.Arrays; import java.util.stream.IntStream; import java.util.stream.Stream; public class MergeArray { public static void main(String[] args) { int first[] = new int[] { 56, 5, 89 }; IntStream stream = Arrays.stream(first); Stream<Integer> firstStream = stream.boxed(); // Integer[]<--Stream<Integer> Convert Stream to Integer[] //Integer[] firstInteger = firstStream.toArray(Integer[]::new); int second[] = { 78, 9, 30 }; // in one line Stream<Integer> secondStream = Arrays.stream(second).boxed(); Integer[] both = Stream.concat(firstStream, secondStream).toArray(Integer[]::new); System.out.println(Arrays.toString(both)); } }OUTPUT: [56, 5, 89, 78, 9, 30]
System.arraycopy is a method you can use to perform this copy.
This is a fast and efficient solution and will work for primitive types.
You should avoid solutions involving ArrayLists, streams, etc as these will need to allocate temporary memory for no useful purpose.
java.util.Arrays; public class MergeArray { public static void main(String[] args) { int first[] = new int[] { 56, 5, 89 }; int second[] = { 78, 9, 30 }; int third[] = new int[first.length + second.length]; System.arraycopy(first, 0, third, 0, first.length); System.arraycopy(second, 0, third, first.length, second.length); System.out.println(Arrays.toString(third)); } }OUTPUT: [56, 5, 89, 78, 9, 30]
Post a Comment