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
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.