2

I have an integer array. int[] arr1 = new int[] {3, 4, 5};. I want to insert this array when initializing another array. So that when int[] arr2 = new int[] {8, 7, arr1, 1, 0};, arr2 will equal {8, 7, 3, 4, 5, 1, 0}.

Also, I do not know the length of arr1, so I can't do int[] arr2 = new int[] {8, 7, arr1[0], arr1[1], arr1[2], 1, 0};

Any help is greatly appreciated.

5
  • 1
    Does this answer your question? Java: How to set an array with another array? Commented Oct 9, 2020 at 8:56
  • 1
    Do you want a one-line solution or is it possible to answer in 2 or 3 lines? Commented Oct 9, 2020 at 8:57
  • 1
    @Milgo no, that doesn't solve my problem. It uses ArrayUtils.addAll. I need the array to be in a specific position in the second array. Commented Oct 9, 2020 at 9:01
  • 1
    there is no native way, write a for loop that iterates over the inner array and sets its values in the outer. Commented Oct 9, 2020 at 9:02
  • @0009laH I would of course prefer a one liner. But if it requires 2 or 3 lines, that's fine as well. Commented Oct 9, 2020 at 9:02

3 Answers 3

4

Sadly as far as I'm aware something like that can't be done in a single operation. The best I was able to come up with is to create a result array. Then you copy to it first part of arr2, whole arr1 and finally second part of arr2. In the end whole method would look like so:

private static int[] insertArrayAtPosition(int[] arr1, int[] arr2, int insertPos){
    int[] result = new int[arr1.length + arr2.length];
    System.arraycopy(arr2, 0, result, 0, insertPos);
    System.arraycopy(arr1, 0, result, insertPos, arr1.length);
    System.arraycopy(arr2, insertPos, result, insertPos + arr1.length, arr2.length - insertPos);
    return result;
}

And then you can call it like so:

public static void main(String args[]) {
    int[] arr1 = new int[] {3, 4, 5};
    int[] arr2 = new int[] {8, 7, 1, 0};
    int[] result = insertArrayAtPosition(arr1, arr2, 2);
    System.out.println(Arrays.toString(result));
}

Edit: Milgo's solution looks much better. However, mine allows to insert one array into another at specific position after they are already created. Which one to use depeds on the usecase.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use ArrayUtils.addAll() three times.

int[] arr1 = new int[] {3, 4, 5};
int[] arr2 = new int[arr1.length + 4];
ArrayUtils.addAll(arr2, new int[]{8, 7});
ArrayUtils.addAll(arr2, arr1);
ArrayUtils.addAll(arr2, new int[]{1, 0});

ArrayUtils is part of Apache Commons.

4 Comments

The class ArrayUtils doesn't seem to exist. I am using Java 8 with no external libraries
It's part of Apache Commons.
Do it work when you explicitely import org.apache.commons.lang.ArrayUtils in your code?
@0009laH No, you would have to download an external jar file first.
0

This is not natively supported in java / there is no language feature that allows you to use a one liner. You can of course write a loop that inserts the elements of the other array, you can also use list interfaces or Arrays#stream. The best way to do it should probably be to use lists, like this:

List<Integer> arr1 = Arrays.asList(3, 4, 5);
List<Integer> arr2 = Arrays.asList(8, 7, 1, 0);
arr2.addAll(2, arr1);

Hope this helpes.

Edit: It is a one liner btw :)

3 Comments

arr1 and arr2 are int[], not List<Integer>. Switching from one to another requires several lines of code
yes, im aware, but i said it could be better to use the List interface. Also, you can "convert" arrays to lists.
You can convert lists to arrays only when they have the same template (List<Integer> and Integer[]), but there is no one-line conversion for List<Integer> and int[]. It would require to cast each element from Integer to int or vice versa. Nonetheless, I agree it would be more adapted to work with List<Integer> in his case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.