how to dynamically declare an array in java code example
Example 1: dynamic array in java
import java.util.Arrays;
public class DynamicArray{
private int array[];
private int size;
private int capacity;
public DynamicArray(){
array = new int[2];
size=0;
capacity=2;
}
public void addElement(int element){
if (size == capacity){
ensureCapacity(2);
}
array[size] = element;
size++;
}
Example 2: how to create dynamic string array in java
ArrayList<String> mylist = new ArrayList<String>();
mylist.add(mystring);
Example 3: how to create dynamic string array in java
List<String> zoom = new ArrayList<>();
zoom.add("String 1");
zoom.add("String 2");
for (String z : zoom) {
System.err.println(z);
}