Java 在两个堆栈的队列中创建toString
我已经使用我创建的堆栈类中的两个堆栈创建了一个队列。我想知道是否有可能按顺序读取队列中的所有元素,而不会使它们从堆栈中丢失 问题是:我能否在MyQueue.java中创建一个toString,它将列出队列的顺序 这是我的两份文件 MyQueue.javaJava 在两个堆栈的队列中创建toString,java,arrays,stack,queue,Java,Arrays,Stack,Queue,我已经使用我创建的堆栈类中的两个堆栈创建了一个队列。我想知道是否有可能按顺序读取队列中的所有元素,而不会使它们从堆栈中丢失 问题是:我能否在MyQueue.java中创建一个toString,它将列出队列的顺序 这是我的两份文件 MyQueue.java import java.util.NoSuchElementException; public class MyQueue<T> { private MyStack<T> stack1; // back o
import java.util.NoSuchElementException;
public class MyQueue<T> {
private MyStack<T> stack1; // back of queue
private MyStack<T> stack2; // front of queue
public MyQueue() {
stack1 = new MyStack<T>();
stack2 = new MyStack<T>();
}
private void moveStack1ToStack2()throws Exception {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
}
public boolean isEmpty() {
return stack1.isEmpty() && stack2.isEmpty();
}
public T peek() throws Exception {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
if (stack2.isEmpty()) moveStack1ToStack2();
T result = stack2.peek();
return result;
}
// add the item to the queue
public void enqueue(T item) throws Exception
{
stack1.push(item);
}
public T dequeue() throws Exception {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
if (stack2.isEmpty())
{
moveStack1ToStack2();
}
return (T)stack2.pop();
}
public int size()
{
return stack1.size() + stack2.size();
}
}
import java.util.NoSuchElementException;
公共类MyQueue{
private MyStack stack1;//队列后面
私有MyStack stack2;//队列前面
公共MyQueue(){
stack1=新MyStack();
stack2=新MyStack();
}
私有void moveStack1ToStack2()引发异常{
而(!stack1.isEmpty())
stack2.push(stack1.pop());
}
公共布尔值为空(){
返回stack1.isEmpty()&&stack2.isEmpty();
}
public T peek()引发异常{
如果(isEmpty())抛出新的NoTouchElementException(“队列下溢”);
如果(stack2.isEmpty())moveStack1ToStack2();
T result=stack2.peek();
返回结果;
}
//将项目添加到队列中
公共无效排队(T项)引发异常
{
堆栈1.推送(项目);
}
public T dequeue()引发异常{
如果(isEmpty())抛出新的NoTouchElementException(“队列下溢”);
if(stack2.isEmpty())
{
moveStack1ToStack2();
}
return(T)stack2.pop();
}
公共整数大小()
{
返回stack1.size()+stack2.size();
}
}
MyStack.java
import java.util.ArrayList;
import java.util.EmptyStackException;
public class MyStack<T> {
private ArrayList<T> al;
public MyStack() {
al = new ArrayList<T>();
}
public void push(T item) {
al.add(item);
}
public T pop() {
if (!isEmpty())
return al.remove(size()-1);
else
throw new EmptyStackException();
}
public boolean isEmpty() {
return (al.size() == 0);
}
public T peek()
{
if (!isEmpty())
return al.get(size()-1);
else
throw new EmptyStackException();
}
public int size() {
return al.size();
}
public String toString()
{
return al.toString();
}
}
import java.util.ArrayList;
导入java.util.EmptyStackException;
公共类MyStack{
私人ArrayList al;
公共MyStack(){
al=新的ArrayList();
}
公共无效推送(T项){
新增(项目);
}
公共广播电台{
如果(!isEmpty())
返回所有移除(大小()-1);
其他的
抛出新的EmptyStackException();
}
公共布尔值为空(){
返回值(al.size()==0);
}
公共T peek()
{
如果(!isEmpty())
返回al.get(size()-1);
其他的
抛出新的EmptyStackException();
}
公共整数大小(){
返回al.size();
}
公共字符串toString()
{
返回al.toString();
}
}
通过添加
public String toString()
{
return stack2.toString();
}
到MyQueue.java
import java.util.NoSuchElementException;
public class MyQueue<T> {
private MyStack<T> stack1; // back of queue
private MyStack<T> stack2; // front of queue
public MyQueue() {
stack1 = new MyStack<T>();
stack2 = new MyStack<T>();
}
private void moveStack1ToStack2()throws Exception {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
}
public boolean isEmpty() {
return stack1.isEmpty() && stack2.isEmpty();
}
public T peek() throws Exception {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
if (stack2.isEmpty()) moveStack1ToStack2();
T result = stack2.peek();
return result;
}
// add the item to the queue
public void enqueue(T item) throws Exception
{
stack1.push(item);
}
public T dequeue() throws Exception {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
if (stack2.isEmpty())
{
moveStack1ToStack2();
}
return (T)stack2.pop();
}
public int size()
{
return stack1.size() + stack2.size();
}
}