Question: I have the following array:

```
int nums[] = new int[]{1,2,3,4,5};

```

I want to add the same elements to the array using stream or another proper way if it is better using stream. There is `addAll()` method for `List`, but I could not use it for array and I am not sure if there is a proper way for array. So, how can I convert this list to:

```
int nums[] = new int[]{1,2,3,4,5,1,2,3,4,5};

```

Answer: 
Based desired output, i'll make a guess that initial array is:

```
int[] nums = new int[]{1, 2, 3, 4, 5};

```

You can't change the size of an array, once created, so you need to declare a new one. @Dan has already offered a [solution](https://stackoverflow.com/a/72336032/17795888) with streams, so i'll give one with loops for completeness sake.

```
public class Temp {

    public static void main(String[] args) throws Exception {
        int[] nums = new int[]{1, 2, 3, 4, 5};
        System.out.println("Initial array");
        System.out.println(Arrays.toString(nums));

        int[] result = new int[nums.length * 2];
        for (int i = 0; i < nums.length; i++) {
            result[i] = nums[i];
            result[i + nums.length] = nums[i];
        }

        System.out.println("Result array");
        System.out.println(Arrays.toString(result));
    }
}

```

The upside is the single iteration used to fill result array.

**Edit:** As per suggestion from @OleV.V. a solution using [System.arraycopy()](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/System.html#arraycopy(java.lang.Object,int,java.lang.Object,int,int)) would look like this:

```
public class Temp {

    public static void main(String[] args) throws Exception {
        int[] nums = new int[]{1, 2, 3, 4, 5};

        int[] result = new int[nums.length * 2];
        System.arraycopy(nums, 0, result, 0, nums.length);
        System.arraycopy(nums, 0, result, nums.length, nums.length);

        System.out.println(Arrays.toString(result));
    }
}

```

The upside is that `System.arraycopy()` is highly optimised and efficient.
how about using `Arrays.copyOf(arg, arg)`?

```
public static void main(String[] args) {
 int[] nums = new int[]{1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
 int[] copiedArray = Arrays.copyOf(nums, nums.length);
}

```

If you really want to use Stream,

```
public static void main(String[] args) {
 int[] nums = new int[]{1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
 int[] copiedArray = Arrays.stream(nums).toArray();
}

```