Answers for "dynamic array in java"

0

how to create dynamic string array in java

ArrayList<String> mylist = new ArrayList<String>();
mylist.add(mystring); //this adds an element to the list.
Posted by: Guest on May-02-2020
0

dynamic array in java

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        ArrayList<Integer> arr=new ArrayList<Integer>(10);

        for(int i =0;i<12;i++){
            arr.add(i);
            System.out.println("VAlUE" + arr.get(i));
            System.out.println("SIZE" + arr.size());
        }
    }
}

// Dynamic nature means even if we initialize the array with
// a lesser size but at run time we need a greater size.
Posted by: Guest on June-06-2021
1

dynamic array in java

import java.util.Arrays;
public class DynamicArray{
    private int array[];
    // holds the current size of array
    private int size;
    // holds the total capacity of array
    private int capacity;
     
    // default constructor to initialize the array and values
    public DynamicArray(){
        array = new int[2];
        size=0;
        capacity=2;
    }
     
    // to add an element at the end
    public void addElement(int element){
        // double the capacity if all the allocated space is utilized
        if (size == capacity){
            ensureCapacity(2); 
        }
        array[size] = element;
        size++;
    }
Posted by: Guest on December-16-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language