Basing on the output you've shown int nums[] = new int[]{1,2,3,4,5,1,2,3,4,5};
and your statement
I want to add the same elements to the array
I think what you were trying to ask was how to re-add the elements of an array to the same array, although your first snippet showed int nums[] = new int[]{4,5,6,7,0,1,2};
while the output was totally unrelated to the first array (might have been a small blunder).
However, in Java it is not possible to update the size of an array. Once an array has been instantiated its size is fixed. What you can do is to declare another array with double the size of the original array you want to copy from.
As it has been pointed out in the comments by @Ole V.V. you could achieve this with IntStream
, which is a better approach than the one I've used since it doesn't force you to change your working type from int[]
to Integer[]
. In fact, the Stream
class works with generic types, so the only argument types it works with are references; therefore you could only retrieve an array of Integer[]
rather than int[]
.
Solution with IntStream
int nums[] = new int[]{1, 2, 3, 4, 5};
int[] res = IntStream.concat(IntStream.of(nums), IntStream.of(nums)).toArray();
System.out.println(Arrays.toString(res));
Solution with Stream
int nums[] = new int[]{1, 2, 3, 4, 5};
Integer[] res = new Integer[0];
res = Stream.concat(Arrays.stream(nums).boxed(), Arrays.stream(nums).boxed())
.collect(Collectors.toList())
.toArray(res);
System.out.println(Arrays.toString(res));