Java实现队列(自定义空满异常、存储与取出数据、是否相同)
通过例子,掌握接口,异常,IO操作等内容。
实现队列
-
队列的实现方式有多种,可以基于数组,也可以基于链表,因此可以把链表需要实现的方法定义为接口。
-
队列在队列满的时候和队列空的时候,不允许写和读操作,可以通过异常实现。
-
队列的数据,可以实现存储和恢复,可以通过文件读取实现。
-
队列可以实现比较是否相同,需要重写equals方法
因此,实现队列,需要定义以下内容:
-
interface JStack
-
class ArrayStack,实现接口,并重写equals方法
-
class StackException
-
class Test
实现代码:
package CurriculumDesign;
import java.io.*;
class StackException extends Exception
{
int index = 0;
public StackException(int index) {
this.index = index;
}
@Override
public String toString() {
return "StackException{ " + "index=" + index + " }";
}
}
interface JStack
{
int outStack() throws StackException;
void inStack(int value) throws StackException;
void save(String fileName) throws IOException;
void load(String fileName) throws IOException;
}
class ArrayStack implements JStack
{
int pos;
int[] data = null;
public ArrayStack() {
pos = -1;
data = new int[32];
}
public ArrayStack(int size) {
pos = -1;
data = new int[size];
}
@Override
public int outStack() throws StackException {
if(pos<0)
{
throw new StackException(pos);
}
int result = data[0];
pos--;
for (int i = 0; i < pos ; i++) {
data[i] = data[i+1];
}
return result;
}
@Override
public void inStack(int value) throws StackException {
if (pos >= (data.length-1))
{
throw new StackException(pos);
}
data[++pos] = value;
}
@Override
public void save(String fileName) throws IOException {
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(fileName));
dataOutputStream.writeInt(pos);
dataOutputStream.writeInt(data.length);
for (int i:data) {
dataOutputStream.writeInt(i);
}
dataOutputStream.flush();
dataOutputStream.close();
System.out.println("save successful!");
}
@Override
public void load(String fileName) throws IOException {
DataInputStream dataInputStream = new DataInputStream(new FileInputStream(fileName));
this.pos = dataInputStream.readInt();
this.data = new int[dataInputStream.readInt()];
for (int i = 0; i < pos; i++) {
data[i] = dataInputStream.readInt();
}
dataInputStream.close();
System.out.println("load successful!");
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof ArrayStack))
{
return false;
}
ArrayStack temp = (ArrayStack) obj;
if ((temp.pos != this.pos) || (temp.data.length != this.data.length))
{
return false;
}
for (int i = 0; i < pos ; i++) {
if(temp.data[i] != this.data[i])
return false;
}
return true;
}
}
public class Test5 {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack(3);
//此时队列为空,取出元素会产生异常
try {
arrayStack.outStack();
} catch (StackException e) {
System.out.println(e.toString());
}
//此时加入插入四个元素(>3)队列会产生异常
try {
arrayStack.inStack(1);
arrayStack.inStack(1);
arrayStack.inStack(1);
arrayStack.inStack(1);
} catch (StackException e) {
System.out.println(e.toString());
}
//尝试取出队列头元素
try {
System.out.println("取出队列头元素的值为:"+arrayStack.outStack());
} catch (StackException e) {
System.out.println(e.toString());
}
//尝试在队列尾插入数值666
try {
arrayStack.inStack(666);
System.out.println("在队列尾插入的值为:666");
} catch (StackException e) {
System.out.println(e.toString());
}
//存储队列当前状态到项目目录下
try {
arrayStack.save("dir");
} catch (IOException e) {
System.out.println(e.toString());
}
//取出当前存储的状态
try {
arrayStack.load("dir");
} catch (IOException e) {
System.out.println(e.toString());
}
//比较两队列是否相同
ArrayStack arrayStack1 = new ArrayStack(3);
for (int i = 0; i <arrayStack1.data.length; i++) {
arrayStack1.data[i] = 1;
}
System.out.println(arrayStack.equals(arrayStack1));
}
}