Wednesday 29 November 2017

Java Program to Implement Variable length array


Code:

import java.util.ArrayList;
import java.util.Scanner;

public class VariableLengthArray
{
    private volatile int size;
    private ArrayList array;

    public VariableLengthArray()
    {
        array = new ArrayList();
        setSize(-1);
    }

    public void setSize(int size)
    {
        this.size = size;
    }

    public int getSize()
    {
        return size;
    }

    public void store(int index, T value)
    {
        try 
        {
            array.set(index, value);
        } catch (IndexOutOfBoundsException indexOutBounds)
        {
            if (index >= 0 && !(index < size))
            {
                throw new IndexOutOfBoundsException();
            }
            array.add(index, value);
        }
    }

    public T get(int index)
    {
        try
        {
            if (index >= 0 && index < size)
                return array.get(index);
            else
                throw new IndexOutOfBoundsException();
        } catch (IndexOutOfBoundsException indexOutBounds)
        {
            System.out.println("INDEX OUT OF BOUNDS : the specified index is 
                     more than the size of the  array");
        }
        return null;
    }

    public T remove(int index)
    {
        try 
        {
            if (index >= 0 && index < size)
            {
                size--;
                return array.remove(index);
            } else
            throw new IndexOutOfBoundsException();
        } catch (IndexOutOfBoundsException indexOutBounds)
        {
            System.out.println("INDEX OUT OF BOUNDS : the specified index
                        is more than the size of the array");
        }
        return null;
    }

    public static void main(String... arg)
    {
        int size, value;
        String choice;
        Scanner scanner = new Scanner(System.in);

        VariableLengthArray integersArray = new VariableLengthArray();

        do
        {
            System.out.println("Enter the size of the array");
            size = scanner.nextInt();

            integersArray.setSize(size);
            System.out.println("Enter the values of the array");
            for (int index = 0; index < integersArray.getSize(); index++)
            {
                value = scanner.nextInt();
                integersArray.store(index, value);
            }

            System.out.println("The Values entered are ");
            for (int index = 0; index < integersArray.getSize(); index++)
            {
                System.out.print(integersArray.get(index) + "\t");
            }

            System.out.println("\nEnter more values ?[y/n]");
            choice = scanner.next();
        } while (choice.equals("y"));
        scanner.close();
    }
}


Output:

Enter the size of the array
5
Enter the values of the array
10 9 8 7 6 
The Values entered are 
10 9 8 7 6
Enter more values ?[y/n]
y
Enter the size of the array
3
Enter the values of the array
2 3 4 
The Values entered are 
2 3 4



More Java Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...