Queue using Array and Class Example in Java
On this page (9sections)
Introduction
Queue Using Array And Class is a classic Java console program that demonstrates the concept with complete source code and sample output. Classic data structures such as stack, queue and linked list implemented in Java.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the line is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue ?dequeue? function. Every time another object or customer enters the line to wait, they join the end of the line and represent the ?enqueue? function. The queue ?size? function would return the length of the line, and the ?empty? function would return true only if there was nothing in the line.
Queue Using Array And Class Example Program
import java.io.*;
class QueueMain {
BufferedReader buffread = new BufferedReader(new InputStreamReader(System.in));
int items[];
int i, front = 0, rear = 0, itemnum, item, count = 0;
void getdata() {
try {
System.out.println("Enter the Limit :");
itemnum = Integer.parseInt(buffread.readLine());
items = new int[itemnum];
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
void enqueue() {
try {
if (count < itemnum) {
System.out.println("Enter Queue Element :");
item = Integer.parseInt(buffread.readLine());
items[rear] = item;
rear++;
count++;
} else {
System.out.println("Queue Is Full");
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
void dequeue() {
if (count != 0) {
System.out.println("Deleted Item :" + items[front]);
front++;
count--;
} else {
System.out.println("Queue Is Empty");
}
if (rear == itemnum) {
rear = 0;
}
}
void display() {
int m = 0;
if (count == 0) {
System.out.println("Queue Is Empty");
} else {
for (i = front; m < count; i++, m++) {
System.out.println(" " + items[i]);
}
}
}
}
class QueueProgram {
public static void main(String arg[]) {
DataInputStream instr = new DataInputStream(System.in);
int choice;
QueueMain que = new QueueMain();
que.getdata();
System.out.println("Queue\n\n");
try {
do {
System.out.println("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
System.out.println("Enter the Choice : ");
choice = Integer.parseInt(instr.readLine());
switch (choice) {
case 1:
que.enqueue();
break;
case 2:
que.dequeue();
break;
case 3:
que.display();
break;
}
} while (choice != 4);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Sample Output
Enter the Limit :
4
Queue
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
1
Enter Queue Element :
45
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
1
Enter Queue Element :
67
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
1
Enter Queue Element :
89
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
1
Enter Queue Element :
567
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
1
Queue Is Full
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
3
45
67
89
567
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
2
Deleted Item :45
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
2
Deleted Item :67
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
2
Deleted Item :89
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
2
Deleted Item :567
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
2
Queue IS Empty
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter the Choice :
4
When to use
Use this queue using array and class example when learning or revising core Java syntax.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
import java.io.*;imports a class used later in the program. -
BufferedReader buffread = new BufferedReader(new InputStreamReader(System.in));updates a variable used in the calculation or output. -
int i, front = 0, rear = 0, itemnum, item, count = 0;updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
itemnum = Integer.parseInt(buffread.readLine());updates a variable used in the calculation or output. -
items = new int[itemnum];updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below.
Best Practices
- Use meaningful variable and class names that describe their purpose.
- Compile and run the program locally — modify values to see how output changes.
- Read compiler errors carefully; they usually point to the exact line to fix.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.