Home > Back-end >  There is a method to dinamically change the size of array in java
There is a method to dinamically change the size of array in java

Time:02-03

I'm a beginner with java and I wanted to know if there was a way to dynamically instantiate an array, that is, being able to add and remove elements by changing the actual size of the array.

CodePudding user response:

In java there is a class called Arraylist, to import it import java.util.ArrayList;

you have to declare it with the data type you want it to contain an example below with some strings

ArrayList <String> cars = new ArrayList <String> ();

To add an item just use the .add (item) function like this

cars.add ("Tesla");

to remove instead it is necessary to have the index and with the command .remove (index) you can remove an object

cars.remove(0);

CodePudding user response:

Try Arraylist: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

List<String> list = new Arraylist();
  •  Tags:  
  • Related