Java Program to Implement Variable Length Array

This Java program is to Implement Variable length array. In programming, a variable-length array (or VLA) is an array data structure of automatic storage duration whose length is determined at run time (instead of at compile time).

Here is the source code of the Java program to implement variable length array. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class VariableLengthArray<T>
  5. {
  6.     private volatile int size;
  7.     private ArrayList<T> array;
  8.  
  9.     public VariableLengthArray()
  10.     {
  11.         array = new ArrayList<T>();
  12.         setSize(-1);
  13.     }
  14.  
  15.     public void setSize(int size)
  16.     {
  17.         this.size = size;
  18.     }
  19.  
  20.     public int getSize()
  21.     {
  22.         return size;
  23.     }
  24.  
  25.     public void store(int index, T value)
  26.     {
  27.         try 
  28.         {
  29.             array.set(index, value);
  30.         } catch (IndexOutOfBoundsException indexOutBounds)
  31.         {
  32.             if (index >= 0 && !(index < size))
  33.             {
  34.                 throw new IndexOutOfBoundsException();
  35.             }
  36.             array.add(index, value);
  37.         }
  38.     }
  39.  
  40.     public T get(int index)
  41.     {
  42.         try
  43.         {
  44.             if (index >= 0 && index < size)
  45.                 return array.get(index);
  46.             else
  47.                 throw new IndexOutOfBoundsException();
  48.         } catch (IndexOutOfBoundsException indexOutBounds)
  49.         {
  50.             System.out.println("INDEX OUT OF BOUNDS : the specified index is 
  51.                      more than the size of the  array");
  52.         }
  53.         return null;
  54.     }
  55.  
  56.     public T remove(int index)
  57.     {
  58.         try 
  59.         {
  60.             if (index >= 0 && index < size)
  61.             {
  62.                 size--;
  63.                 return array.remove(index);
  64.             } else
  65.             throw new IndexOutOfBoundsException();
  66.         } catch (IndexOutOfBoundsException indexOutBounds)
  67.         {
  68.             System.out.println("INDEX OUT OF BOUNDS : the specified index
  69.                         is more than the size of the array");
  70.         }
  71.         return null;
  72.     }
  73.  
  74.     public static void main(String... arg)
  75.     {
  76.         int size, value;
  77.         String choice;
  78.         Scanner scanner = new Scanner(System.in);
  79.  
  80.         VariableLengthArray<Integer> integersArray = new VariableLengthArray<Integer>();
  81.  
  82.         do
  83.         {
  84.             System.out.println("Enter the size of the array");
  85.             size = scanner.nextInt();
  86.  
  87.             integersArray.setSize(size);
  88.             System.out.println("Enter the values of the array");
  89.             for (int index = 0; index < integersArray.getSize(); index++)
  90.             {
  91.                 value = scanner.nextInt();
  92.                 integersArray.store(index, value);
  93.             }
  94.  
  95.             System.out.println("The Values entered are ");
  96.             for (int index = 0; index < integersArray.getSize(); index++)
  97.             {
  98.                 System.out.print(integersArray.get(index) + "\t");
  99.             }
  100.  
  101.             System.out.println("\nEnter more values ?[y/n]");
  102.             choice = scanner.next();
  103.         } while (choice.equals("y"));
  104.         scanner.close();
  105.     }
  106. }


$javac VariableLengthArray.java
$java VariableLengthArray
 
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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

👉 For weekly programming practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels

advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations