1import java.util.ArrayList;
2
3public class Main {
4
5 public static void main(String[] args) {
6 ArrayList<Integer> arr=new ArrayList<Integer>(10);
7
8 for(int i =0;i<12;i++){
9 arr.add(i);
10 System.out.println("VAlUE" + arr.get(i));
11 System.out.println("SIZE" + arr.size());
12 }
13 }
14}
15
16// Dynamic nature means even if we initialize the array with
17// a lesser size but at run time we need a greater size.
1ArrayList<String> mylist = new ArrayList<String>();
2mylist.add(mystring); //this adds an element to the list.
1import java.util.Arrays;
2public class DynamicArray{
3 private int array[];
4 // holds the current size of array
5 private int size;
6 // holds the total capacity of array
7 private int capacity;
8
9 // default constructor to initialize the array and values
10 public DynamicArray(){
11 array = new int[2];
12 size=0;
13 capacity=2;
14 }
15
16 // to add an element at the end
17 public void addElement(int element){
18 // double the capacity if all the allocated space is utilized
19 if (size == capacity){
20 ensureCapacity(2);
21 }
22 array[size] = element;
23 size++;
24 }