Implementation of a Circular Resizable Array in Java
Last Updated :
28 Apr, 2025
A Circular Resizable Array is a data structure that effectively maintains a fixed-size array by enabling members to be added or withdrawn circularly. It is sometimes referred to as a circular buffer or ring buffer. This cyclical behavior is especially helpful in situations when a smooth array wrap is required and a continuous data flow is anticipated. In this tutorial, we will look at how to create a circular resizable array in Java.
Prerequisites:
To comprehend and create a circular resizable array in Java, you must have a basic grasp of Java programming principles, such as Arrays, Loops, and knowledge of data flow.
Circular Resizable Array
The fundamental principle underlying a circular resizable array is keeping two pointers, one for the array's head and another for its tail. When items are added or withdrawn, these pointers travel in a circle within the array's boundaries. The array may dynamically expand itself to make room for extra items as it fills up.
Implementation:
Let's implement a basic version of a circular resizable array in Java.
Java
// Java Program to Implement
// Circular Resizable Array
public class CircularArray {
private int[] array;
private int size;
private int head;
private int tail;
// Constructor
public CircularArray(int capacity) {
array = new int[capacity];
size = 0;
head = 0;
tail = 0;
}
// Enqueue (Add Element) Method
public void enqueue(int element) {
if (size == array.length) {
resizeArray();
}
array[tail] = element;
tail = (tail + 1) % array.length;
size++;
}
// Dequeue (Remove Element) Method
public int dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Circular array is empty");
}
int removedElement = array[head];
head = (head + 1) % array.length;
size--;
return removedElement;
}
// ResizeArray Method
private void resizeArray() {
int newCapacity = array.length * 2;
int[] newArray = new int[newCapacity];
for (int i = 0; i < size; i++) {
newArray[i] = array[(head + i) % array.length];
}
array = newArray;
head = 0;
tail = size;
}
// Check if CircularArray is empty
public boolean isEmpty() {
return size == 0;
}
// Main method for testing
public static void main(String[] args) {
CircularArray circularArray = new CircularArray(5);
circularArray.enqueue(1);
circularArray.enqueue(2);
circularArray.enqueue(3);
circularArray.enqueue(4);
circularArray.enqueue(5);
System.out.println("Dequeuing: " + circularArray.dequeue());
System.out.println("Dequeuing: " + circularArray.dequeue());
circularArray.enqueue(6);
circularArray.enqueue(7);
System.out.println("Dequeuing: " + circularArray.dequeue());
}
}
OutputDequeuing: 1
Dequeuing: 2
Dequeuing: 3
Explaination of the above Program
1. Defining Class:
CircularArray is a Java class that is defined in the code.
public class CircularArray
2. Instance Variables:
private int[] array;
private int size;
private int head;
private int tail;
The circular array is managed by these private instance variables:
- array: The element-storing underlying array.
- size: The circular array's current element count.
- head: Indicates where the circular array's front is located.
- tail: A pointer pointing to the circular array's back.
3. Constructor:
public CircularArray(int capacity) {
array = new int[capacity];
size = 0;
head = 0;
tail = 0;
}
The circular array is initialized with a specified capacity by the class's constructor. At first, the head and tail are both set to 0, as is the size.
4. Enqueue Method:
public void enqueue(int element) {
if (size == array.length) {
resizeArray();
}
array[tail] = element;
tail = (tail + 1) % array.length;
size++;
}
- An element is added to the circular array using the enqueue function.
- The resizeArray function is called to double the array's capacity if size = array.length, indicating that the array is full.
- The tail is updated in a cyclical fashion once the new element is introduced at that place.
5. Dequeue Method:
public int dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Circular array is empty");
}
int removedElement = array[head];
head = (head + 1) % array.length;
size--;
return removedElement;
- The element from the front of the circular array is removed and returned using the dequeue function.
- An exception is raised in case the array is empty.
- The size decreases as the head pointer is rotated in a circular motion.
6. Resize Array Method:
private void resizeArray() {
int newCapacity = array.length * 2;
int[] newArray = new int[newCapacity];
for (int i = 0; i < size; i++) {
newArray[i] = array[(head + i) % array.length];
}
array = newArray;
head = 0;
tail = size;
}
- When the array is filled, the resizeArray function is invoked.
- It replicates the current items, generates a new array twice as large, and modifies the array references for the head, tail, and elements.
7. isEmpty Method:
public boolean isEmpty() {
return size == 0;
}
The circular array's empty status is determined via the isEmpty method.
Main Method for Testing:
Java
public static void main(String[] args)
{
CircularArray circularArray = new CircularArray(5);
circularArray.enqueue(1);
circularArray.enqueue(2);
circularArray.enqueue(3);
circularArray.enqueue(4);
circularArray.enqueue(5);
System.out.println("Dequeuing: "
+ circularArray.dequeue());
System.out.println("Dequeuing: "
+ circularArray.dequeue());
circularArray.enqueue(6);
circularArray.enqueue(7);
System.out.println("Dequeuing: "
+ circularArray.dequeue());
}
- The primary method queues and dequeues items to show how to use the circular array.
- The components that are being dequeued are output to the console.
Similar Reads
How to Implement a Thread-Safe Resizable Array in Java? Multiple threads may securely execute operations like insertion and deletion without risking data corruption when utilizing a thread-safe resizable array. The ArrayList class is a popular Java class, yet it is not thread-safe by default. We may use concurrent collections or synchronization to make i
2 min read
How to Implement a Circular Queue in Java ? A Circular Queue is a queue in which we can insert an element at the start of the Array even if our rare is reached at the last index and if we have space at the start of the array. This reduces the problem of inefficient use of array space. Once the array is filled till the last and if we have spac
4 min read
Java Program to Implement Circular Buffer When data is constantly moved from one place to another or from one process to another or is frequently accessed, it cannot be stored in permanent memory locations such as hard drives as they take time to retrieve the data. This type of data needs to be accessed quickly and is stored in temporary me
8 min read
How to Clone a 2D Array With Different Row Sizes in Java? 2D arrays are two-dimensional arrays. These are the simplest forms of multidimensional arrays. Java provides various methods to clone the arrays, but when dealing with 2D arrays having varying sizes, then the process becomes more difficult. Here, we will see different methods to clone 2D arrays with
5 min read
Java Program to Implement ArrayDeque API The ArrayDeque in Java provides how to use resizable-array additionally to the implementation of the Deque interface. It is also referred to as Array Double Ended Queue or Array Deck. This is a special quite array that grows and allows users to feature or remove a component from each side of the que
3 min read
Java Program to Cyclically Permute the Elements of an Array Given an array of integers, there we cyclically permute its elements, that is, shift each array element to the left by one index. The first value will go into the last index. Example: Input: [1,2,3,4,5] Output: [2,3,4,5,1] Input: [2,3,1,5,6] Output: [3,1,5,6,2] Approach #1 In function cyclicShift(),
4 min read
Determine the Upper Bound of a Two Dimensional Array in Java Multidimensional arrays in Java are common and can be termed as an array of arrays. Data in a two-dimensional array in Java is stored in 2D tabular form. A two-dimensional array is the simplest form of a multidimensional array. A two-dimensional array can be seen as an array of the one-dimensional a
3 min read
How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co
2 min read
How to Implement a Generic Deep Copy Method for Multidimensional Arrays in Java? In Java, two types of copies can be made of any data structure that are Deep copy and Shallow copy. When we make a deep copy of an array, the array is assigned a new memory space, and the content is copied into the new memory space. This means that if we make changes in any of the two arrays it will
3 min read
Difference between multidimensional array in C++ and Java Prerequisites: Multidimensional array in C++, Multidimensional array in Java Multidimensional Arrays: Multidimensional arrays are a tabular representation of arrays to store multiple elements. These dimensions can be 1D arrays, 2D-arrays, etc. Multidimensional arrays are available in both C++ and Ja
4 min read