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.
import java.util.ArrayList;
import java.util.Scanner;
public class VariableLengthArray<T>
{
private volatile int size;
private ArrayList<T> array;
public VariableLengthArray()
{
array = new ArrayList<T>();
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<Integer> integersArray = new VariableLengthArray<Integer>();
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();
}
}
$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
Related posts:
Comparing Objects in Java
Guide to java.util.Formatter
Java Program to Check Whether Graph is DAG
Java Program to Construct an Expression Tree for an Infix Expression
New Features in Java 13
Copy a List to Another List in Java
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Giới thiệu HATEOAS
Command-Line Arguments in Java
JPA/Hibernate Persistence Context
Spring 5 Functional Bean Registration
Upload and Display Excel Files with Spring MVC
How to Add a Single Element to a Stream
Creating a Web Application with Spring 5
Spring Boot - Servlet Filter
Java Program to Perform Stooge Sort
Java 8 Streams peek() API
Java – Reader to String
Giới thiệu Google Guice – Dependency injection (DI) framework
Spring MVC Setup with Kotlin
Introduction to Apache Commons Text
Java Program to Implement Repeated Squaring Algorithm
Java Program to Implement Heap Sort Using Library Functions
Introduction to Java 8 Streams
Guide to @ConfigurationProperties in Spring Boot
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java – Generate Random String
Java Program to Implement Fermat Factorization Algorithm
Guide to java.util.concurrent.Future
An Intro to Spring Cloud Contract
Class Loaders in Java
Giới thiệu Swagger – Công cụ document cho RESTfull APIs