1

As the title, how can I resize array in java, without importing any library, including ArrayList, Arrays.copyOf ...etc.

Also, I have to resize again and again in my program, with the unknown input data.( i.e. I have a small array, but when the data increasing, how can I resize it?)

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
KUAN
  • 27
  • 1
  • 6
  • Does `System` count as an import? If so, your only approach is to allocate a new array with the larger size, and then copy each element from the old to the new. It is not a "resize" but a new array with the same contents. – KevinO Apr 19 '17 at 15:05
  • But creating, copying and replacing is a way to slow, and so you have the choice between : Using an ArrayList, or, creating a big array, so that you many free spaces that are `null`, and when there is a new Element, simply fill it in the next free slot, note I wont use int[] array but Integer[] then cause int, double, float etc. cant be null, so you cant determine if the slot is free or the 0 is data, while Double etc can be null. Then you can just go through the array, and break when a slot is null. Note:Dont break when you delete elements in arrays body. If full, allocate more `buffer` slots – Luatic Apr 19 '17 at 15:58

5 Answers5

4

Unfortunately, you cant resize an array in java.

Some things you could do are:

  1. When new data is taken in, make a new array and copy the old array + the new data.

  2. Use an ArrayList which makes the array bigger as you add data to it.

  3. Use java.util.Arrays.copyOf(...). This returns a bigger array with the contents of the previous array.

Community
  • 1
  • 1
Carter Brainerd
  • 199
  • 2
  • 10
3

Java does not have a "resize" option for allocated storage. You must allocate new storage of the required size and copy the data from the old storage to the new.

You can use System.arraycopy() to copy the data. The ArrayList and Vector classes automatically perform these reallocate and copy operations, so if you don't use them, you are reinventing the wheel.

java.lang.System is, of course, automatically imported into every java program, so, as required, you don't need import java.lang.System; to implement this.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
0

a array is by definition fixed in size, I know many modern languages let you assign the length and change the length dynamically

but I think what you are looking for is a Vector

https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html

it allows O(1) access (like the array) while beeing dynamic in length

subkonstrukt
  • 446
  • 2
  • 15
  • Thanks for your answer. But I have restrict to importing anything in my program. Of course, vector, too. btw thanks! – KUAN Apr 19 '17 at 15:01
  • well, you still might consider a different choice of datastructure if the length is unkown before, depending on your usecase, wherever you have to have random access or not and how fast you need to go trough the collection maybe a single linked list will do; but then again I dont know your requirements (: – subkonstrukt Apr 19 '17 at 15:09
0

You can not increase the size of an array once it is declared. So you have to use Collection framework in java, may be ArrayList or LinkedList.

That is what is the difference between Array and ArrayList. In ArrayList you can add element dynamically,no need to increase it's size.

KOUSIK MANDAL
  • 2,002
  • 1
  • 21
  • 46
0

you could use forEach to copy the values to the enlarged new array

IntStream.range( 0, oldArray.length ).forEach( i -> newArray[i] = oldArray[i] );

Kaplan
  • 2,572
  • 13
  • 14