Java Program to Implement Dynamic Array

«
»
This is a Java Program to implement Dynamic Array. A dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed.

Here is the source code of the Java Program to implement Dynamic Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to implement Dynamic Array
  3.  **/
  4.  
  5. import java.util.Scanner;
  6. import java.util.ArrayList;
  7.  
  8. /** class DynamicArray */
  9. class DynamicArray
  10. {
  11.     private ArrayList<String> al;
  12.  
  13.     /** constructor **/
  14.     public DynamicArray()
  15.     {
  16.         al = new ArrayList<String>();        
  17.     }    
  18.     /** function to clear **/
  19.     public void clear()
  20.     {
  21.         al.clear();
  22.     }
  23.     /** function to get size **/
  24.     public int size()
  25.     {
  26.         return al.size();
  27.     }   
  28.     /** function to insert element **/
  29.     public void insert(String key)
  30.     {
  31.         al.add(key);
  32.     }    
  33.     /** function to get element at index **/
  34.     public String get(int index)
  35.     {
  36.         if (index >= al.size())
  37.             return "";
  38.         return al.get(index);
  39.     }
  40.     /** function to remove element at index **/
  41.     public void remove(int index)
  42.     {
  43.         if (index >= al.size())
  44.             return ;
  45.         al.remove(index);
  46.     }   
  47.     /** function to remove element **/
  48.     public void remove(String key)
  49.     {
  50.         al.remove(key);
  51.     } 
  52.     /** function to display array **/
  53.     public void display()
  54.     {
  55.         System.out.println("\nDynamic Array : "+ al);
  56.         System.out.println();
  57.     }              
  58. }
  59.  
  60. /** Class DynamicArrayTest **/
  61. public class DynamicArrayTest
  62. {
  63.     public static void main(String[] args)
  64.     {
  65.         Scanner scan = new Scanner(System.in);
  66.         System.out.println("Dynamic Array Test\n");   
  67.  
  68.         DynamicArray da = new DynamicArray();
  69.  
  70.         char ch;
  71.         /*  Perform Dynamic Array operations */
  72.         do    
  73.         {
  74.             System.out.println("\nDynamic Array\n");
  75.             System.out.println("1. insert ");
  76.             System.out.println("2. remove by index");
  77.             System.out.println("3. remove by val");
  78.             System.out.println("4. clear");
  79.             System.out.println("5. size");
  80.  
  81.             int choice = scan.nextInt();            
  82.             switch (choice) 
  83.             {
  84.             case 1 : 
  85.                 System.out.println("Enter value to insert");
  86.                 da.insert(scan.next() );                     
  87.                 break;                          
  88.             case 2 : 
  89.                 System.out.println("Enter index");
  90.                 da.remove(scan.nextInt() );
  91.                 break;        
  92.             case 3 : 
  93.                 System.out.println("Enter value");
  94.                 da.remove(scan.next() );
  95.                 break;                                   
  96.             case 4 : 
  97.                 System.out.println("\nDynamic Array Cleared");
  98.                 da.clear();
  99.                 break;    
  100.             case 5 : 
  101.                 System.out.println("\nSize = "+ da.size() );
  102.                 break;         
  103.             default : 
  104.                 System.out.println("Wrong Entry \n ");
  105.                 break;   
  106.             }    
  107.             da.display();    
  108.  
  109.             System.out.println("\nDo you want to continue (Type y or n) \n");
  110.             ch = scan.next().charAt(0);                        
  111.         } while (ch == 'Y'|| ch == 'y');               
  112.     }
  113. }

advertisement
Dynamic Array Test
 
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
1
Enter value to insert
apple
 
Dynamic Array : [apple]
 
 
Do you want to continue (Type y or n)
 
y
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
1
Enter value to insert
mango
 
Dynamic Array : [apple, mango]
 
 
Do you want to continue (Type y or n)
 
y
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
1
Enter value to insert
banana
 
Dynamic Array : [apple, mango, banana]
 
 
Do you want to continue (Type y or n)
 
y
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
1
Enter value to insert
strawberry
 
Dynamic Array : [apple, mango, banana, strawberry]
 
 
Do you want to continue (Type y or n)
 
y
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
5
 
Size = 4
 
Dynamic Array : [apple, mango, banana, strawberry]
 
 
Do you want to continue (Type y or n)
 
y
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
2
Enter index
2
 
Dynamic Array : [apple, mango, strawberry]
 
 
Do you want to continue (Type y or n)
 
y
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
3
Enter value
strawberry
 
Dynamic Array : [apple, mango]
 
 
Do you want to continue (Type y or n)
 
y
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
5
 
Size = 2
 
Dynamic Array : [apple, mango]
 
 
Do you want to continue (Type y or n)
 
y
 
Dynamic Array
 
1. insert
2. remove by index
3. remove by val
4. clear
5. size
4
 
Dynamic Array Cleared
 
Dynamic Array : []
 
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

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 & technical discussions at Telegram SanfoundryClasses.