Skip to content
Advertisement

How to add new elements to an array?

I have the following code:

JavaScript

Those two appends are not compiling. How would that work correctly?

Advertisement

Answer

The size of an array can’t be modified. If you want a bigger array you have to instantiate a new one.

A better solution would be to use an ArrayList which can grow as you need it. The method ArrayList.toArray( T[] a ) gives you back your array if you need it in this form.

JavaScript

If you need to convert it to a simple array…

JavaScript

But most things you do with an array you can do with this ArrayList, too:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement