Friday, January 17, 2025
HomeGeneralHow can I concatenate two arrays in Java?

How can I concatenate two arrays in Java?

You can concatenate two arrays in Java using the following methods:

Method 1: Using Arrays. copy Of and System. array copy

import java.util.Arrays;

Method 2: Using Java 8 Streams

import java.util.Arrays;
import java.util.stream.IntStream;

Public class Main {public static void main (String [] args) {int[] array1 = {1, 2, 3}
int[] array2 = {4, 5, 6}; int[] result = IntStream.concat (Arrays.stream(array1), Arrays.stream(array2)).toArray();System.out.println (Arrays.toString(result))

See also  What Physical Object Would be a Symbol of Unity?

Method 3: Using Apache Commons Lang
If you’re using Apache Commons Lang, you can use the ArrayUtils.addAll method: import org.apache.commons.lang3.ArrayUtils;

Public class Main {public static void main(String[] args) {int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};int[] result = ArrayUtils.addAll(array1, array2); System.out.println(Arrays.toString(result));

See also  What's The Best Credit Card For Groceries?

These methods will all produce the same result.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x