Jump to content

self-expanding array

stefanmz
Go to solution Solved by Slottr,

If you're using a standard Java array, you can write a method to create a new array and return the new array with the additional value

 

Alternatively, an easier solution would be to just use a Java library object like an ArrayList that supports these kinds of functions natively 

Hey,how can I create a self-expanding array in java? I don't know the exact length because it may vary and I need it to auto-expand when it runs out of bounds.

 

Link to comment
Share on other sites

Link to post
Share on other sites

If you're using a standard Java array, you can write a method to create a new array and return the new array with the additional value

 

Alternatively, an easier solution would be to just use a Java library object like an ArrayList that supports these kinds of functions natively 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

49 minutes ago, Slottr said:

If you're using a standard Java array, you can write a method to create a new array and return the new array with the additional value

 

Alternatively, an easier solution would be to just use a Java library object like an ArrayList that supports these kinds of functions natively 

yeah definitely Array List otherwise I might have hundreds of arrays in some cases

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, Slottr said:

If you're using a standard Java array, you can write a method to create a new array and return the new array with the additional value

 

Alternatively, an easier solution would be to just use a Java library object like an ArrayList that supports these kinds of functions natively 

thanks for suggesting it , ArrayList is awesome! It works flawlessly

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, stefanmz said:

yeah definitely Array List otherwise I might have hundreds of arrays in some cases

Not if you do it properly. You create a new (larger) array, copy the existing values into it, add the new value, then "throw away" the old array (i.e. no more references to it, so the garbage collector will take care of it).

// New array with a larger size
final var newArray = new String[this.array.length * 2];

// Copy values to the new array
System.arraycopy(
  this.array,         // the array to copy from
  0,                  // the starting position
  newArray,           // the array to copy to
  0,                  // the starting position
  this.array.length   // the number of items to copy
);

// Add the new item
newArray[this.array.length] = newItem;

// Old array is now out of scope and can be garbage collected
this.array = newArray;

 

That's pretty much how ArrayList<T> works internally. When you create a new instance, it'll create a small array (e.g. new T[10]). When you add more items, it'll create a new array (e.g. x2 in size), copy the values over, then replace the old array with the new one. Note that the initial size and growth factor are only examples and may differ between Java versions and/or the developer (e.g. Oracle vs OpenJDK).

 

It's always a good idea to use "new ArrayList<>(someSize)" if you have a rough idea how large your list will get. It'll avoid needless copying and resizing any time you exceed the old internal capacity.

 

E.g. if you add one more item to a list whose array is at maximum capacity, it might create room for many more items, even though you'll never need it. That's why functions such as ".trimToSize()" exist. Once you know you'll add no more items, you can use it to ensure the internal array is only as large as it needs to be. Something to keep in mind when you work with lots of lists that grow dynamically.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×