我在Java中设计了一个循环队列程序,它将在特定时间显示队列中存在的元素。
以下是代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Queue;
public class CircularQueue {
private int maxSize;
private int[] cq;
private int front = -1;
private int rear = -1;
private int count = 0;
private void initialize(int maxSize) {
this.maxSize = maxSize;
cq = new int[this.maxSize];
System.out.println("Circular Queue Initialized with the size of " + cq.length);
}
private void enqueue(int element) {
if (front == (rear + 1) % this.maxSize) {
System.out.println("Queue is already Full");
System.out.println("error : Queue Overflows.");
} else {
rear = (rear + 1) % this.maxSize;
cq[rear] = element;
count++;
}
if (front == -1) {
front = 0;
}
}
private int dequeue() {
int element = 0;
if ((front == rear) && (rear == -1)) {
System.out.println("Queue is already Empty");
System.out.println("Error: Queue UnderFlows.");
} else {
element = cq[rear];
count--;
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % this.maxSize;
}
}
return element;
}
private void listQueue() {
if (rear > front) {
for (int i = front; i <= rear; i++) {
System.out.println(cq[i]);
}
} else if (rear == front) {
System.out.println(cq[rear]);
} else if (rear < front) {
for (int i = front; i < this.maxSize; i++) {
System.out.println(cq[i]);
}
for (int i = 0; i <= rear; i++) {
System.out.println(cq[i]);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CircularQueue obj = new CircularQueue();
obj.initialize(2);
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.enqueue(10);
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.enqueue(20);
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
// obj.enqueue(30);
// System.out.println("count : " + obj.count);
// System.out.println("front : " + obj.front + " : rear : " + obj.rear);
// System.out.println("");
obj.listQueue();
System.out.println("");
obj.dequeue();
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.listQueue();
System.out.println("");
obj.enqueue(10);
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.listQueue();
System.out.println("");
obj.dequeue();
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.listQueue();
System.out.println("");
obj.dequeue();
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.dequeue();
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
}
}
现在,在我的程序中我想要的是,当我想知道Queue中存在的元素的那一刻,它应该在文件中保存从队列的前端到后端开始的那些元素,以便我可以检查随时归档。
但是,有一点需要记住,应该每次更新存储循环队列中的元素的文件。
要在文件中存储此print.ln()
命令,我的代码如下:
try {
PrintStream out = new PrintStream(new FileOutputStream(
"C://JavaPrograms/ListDeclared.txt"));
for (int i = 0; i < size; i++)
out.println(""count : " + obj.count);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
现在,问题是我必须在每个S.O.P语句之后包含此Try-Catch Block以便保存在文件中。那么,还有其他选择。
请提前帮助,谢谢。 :)