Can I copy an array to itself without creating a new array object (purpose of increase it size)?
I know that with using Collections I will have no such problems, so you don't need to remind me about Collections usage to solve this problem.
I was sure that Arrays.copyOf
and System.arraycopy
creates a new objects for this purpose and leave old array object in heap.
But today I was googling and found that question at stack How to effectively copy an array in java? there written that System.arraycopy
doesn't create new object when copies an array, it copies into existing array. I was wonder, when I read this and suggest that this will work for copyring array into itself but I've not found appropriative example to do this.
So there are a way to copy an array to itself with increasing it size without creating new array object(that heap will hold) or not?

- 1
- 1

- 1,319
- 3
- 14
- 31
-
1*Why* would you want to do that?! – Biffen Feb 23 '15 at 15:15
-
I want to know it's can be done or not? – Mike Herasimov Feb 23 '15 at 15:17
-
1As the answer says, it is impossible. Once created, an array in Java has a fixed size which cannot be changed. – fge Feb 23 '15 at 15:18
2 Answers
System.arraycopy can be used to copy an array to iteself.
From the Javadoc :
* If the <code>src</code> and <code>dest</code> arguments refer to the
* same array object, then the copying is performed as if the
* components at positions <code>srcPos</code> through
* <code>srcPos+length-1</code> were first copied to a temporary
* array with <code>length</code> components and then the contents of
* the temporary array were copied into positions
* <code>destPos</code> through <code>destPos+length-1</code> of the
* destination array.
However, copying an array to itself wouldn't help you increase its size, since you can't increase the size of an array. You must create a new, larger, array and copy the elements of the original array to the new array.
-
-
-
@MichaelH.System.arraycopy doesn't create a new object. You have to create the new array and pass it to arraycopy. – Eran Feb 23 '15 at 15:16
-
I throught it can be done because of `Arrays.copyOf` can copy array to itself(but it use `System.arraycopy` inside of it) – Mike Herasimov Feb 23 '15 at 15:21
-
@MichaelH. Arrays.copyOf creates a new array, copies the original array to it and returns the new array. – Eran Feb 23 '15 at 15:23
-
Year I mean this. So in any way (using a `Array.copyOf` or `System.arraycopy`) we need or system by default to create new array? I clearly understand? – Mike Herasimov Feb 23 '15 at 15:27
-
@MichaelH. Yes, if you require a larger array, you must create a new larger array (or call Arrays.copyOf which would do it for you). – Eran Feb 23 '15 at 15:28
-
maybe you know about performance? what is better to use? System.arraycopy work's faster? – Mike Herasimov Feb 23 '15 at 15:36
-
-
-
@MichaelH. Arrays.copyOf calls System.arraycopy, so they should both have the same performance. – Eran Feb 23 '15 at 15:40
-
Can I copy an array to itself without creating a new array object (purpose of increase it size)?
Yes, you can copy an array to itself.
No, you can't increase (or decrease) the size of an array. Copying an array to itself won't achieve that.
The size of an array cannot be changed. If you need the array to be larger or smaller than it currently is, you need to create a new array, and copy from the old to the new as appropriate.
I was sure that Arrays.copyOf and System.arraycopy creates a new objects for this purpose and leave old array object in heap.
Arrays.copyOf creates a new array.
System.arraycopy doesn't create a new array ... but it doesn't change the size of any existing array either.
So there are a way to copy an array to itself with increasing it size without creating new array object(that heap will hold) or not?
There isn't.

- 698,415
- 94
- 811
- 1,216