Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to Implement Simple CircularArrayList in Java?

$
0
0

java-circulararraylist-example

In this tutorial we will go over details on how to implement Circular ArrayList. There are numerous examples we have on Crunchify about ArrayList and this one will be with Integer Array.

Let’s get started:

  • Create class CrunchifyCircularArrayList.java
  • Create constructor with only one parameter arraySize which is 10 in our case
  • In main method, we are going to add 15 element which will be random Integer Element
  • We will print log which we will add and print size too
  • After 10th element, crunchifyArray size will be be stable at 10
  • We will sort and log crunchifyArray which will have only last 10 elements

package crunchify.com.tutorial;

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyCircularArrayList {

	private static Integer[] crunchifyArray;
	final private int arraySize;
	private static int first = 0;
	private int last = 0;
	private static int length = 0;
	static int sizeForDemo = 10;

	public CrunchifyCircularArrayList(int arraySize) {
		super();
		this.arraySize = arraySize;
		CrunchifyCircularArrayList.crunchifyArray = new Integer[arraySize];
	}

	public static void main(String args[]) {
		CrunchifyCircularArrayList crunchifyObj = new CrunchifyCircularArrayList(sizeForDemo);

		for (int i = 1; i <= 15; i++) {
			int crunchifyInteger = ThreadLocalRandom.current().nextInt(1, 50);
			log("Adding element: " + crunchifyInteger);
			crunchifyObj.put(crunchifyInteger);
			log("Length: " + length);
		}

		log("\nHere is sorted ArrayList (last 10 elements): ");
		Integer[] sortedArray = getSortedArrayList();

		for (int crunchifyArrayVal : sortedArray) {
			log(crunchifyArrayVal + " ");
		}
	}

	// Simple Log Utility
	private static void log(Object value) {
		System.out.println(value);

	}

	// Get First Element
	public static int getFront() {
		return first;
	}

	// Put element into Circular ArrayList
	public synchronized void put(int element) {
		crunchifyArray[last] = element;
		if (length < arraySize) {
			length++;
		} else {
			first = (first + 1) % arraySize;
		}
		last = (last + 1) % arraySize;
	}

	// Sort crunchifyArray
	public static Integer[] getSortedArrayList() {
		if (length == 0) {
			return null;
		}
		Integer[] newArray = new Integer[length];
		System.arraycopy(crunchifyArray, 0, newArray, 0, length);
		Arrays.sort(newArray);
		return Arrays.copyOf(newArray, sizeForDemo);
	}

	// Get array Length
	public int getLength() {
		return length;
	}
}

Eclipse console Output:

Adding element: 36
Length: 1
Adding element: 28
Length: 2
Adding element: 23
Length: 3
Adding element: 31
Length: 4
Adding element: 4
Length: 5
Adding element: 28
Length: 6
Adding element: 11
Length: 7
Adding element: 44
Length: 8
Adding element: 8
Length: 9
Adding element: 31
Length: 10
Adding element: 40
Length: 10
Adding element: 34
Length: 10
Adding element: 49
Length: 10
Adding element: 1
Length: 10
Adding element: 37
Length: 10

Here is sorted ArrayList (last 10 elements): 
1 
8 
11 
28 
31 
34 
37 
40 
44 
49

Have a suggestion on article? Please chime in and share it as a comment.

The post How to Implement Simple CircularArrayList in Java? appeared first on Crunchify.

Author: App Shah

Crunchify, LLC Logo


Viewing all articles
Browse latest Browse all 1037

Trending Articles