Java 过流器
我正在做一些关于实现接口的学校练习,我已经得到了为我提供的工作空间和测试课程。我要做的基本上是实现这些方法,运行测试类,并确保它们通过测试 这是我的密码: 以下是测试课程,以防你想看到学校提供的,所以我怀疑它们是错误的: 因此,当我尝试运行测试时,在queue.FifiQueue.FifoQueue.java:10处会出现多个StackOverflower错误 我已经看了好几个小时的代码了,但是不知道我在哪里写了不好的代码Java 过流器,java,stack-overflow,Java,Stack Overflow,我正在做一些关于实现接口的学校练习,我已经得到了为我提供的工作空间和测试课程。我要做的基本上是实现这些方法,运行测试类,并确保它们通过测试 这是我的密码: 以下是测试课程,以防你想看到学校提供的,所以我怀疑它们是错误的: 因此,当我尝试运行测试时,在queue.FifiQueue.FifoQueue.java:10处会出现多个StackOverflower错误 我已经看了好几个小时的代码了,但是不知道我在哪里写了不好的代码 PS:我知道一些方法的实现不正确,我稍后会尝试修复它们,但现在我只是尝试
PS:我知道一些方法的实现不正确,我稍后会尝试修复它们,但现在我只是尝试传递提供的och大小方法。您的构造函数正在调用自身,导致无限递归:
public FifoQueue() {
queue = new FifoQueue<E>();
}
也许您想在内部包装其他类型的队列或列表?可能重复的
package testqueue;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.NoSuchElementException;
import java.util.Iterator;
import queue.FifoQueue;
public class TestFifoQueue {
private FifoQueue<Integer> myIntQueue;
private FifoQueue<String> myStringQueue;
@Before
public void setUp() throws Exception {
myIntQueue = new FifoQueue<Integer>();
myStringQueue = new FifoQueue<String>();
}
@After
public void tearDown() throws Exception {
myIntQueue = null;
myStringQueue = null;
}
/**
* Test if a newly created queue is empty.
*/
@Test
public final void testNewFifoQueue() {
assertTrue(myIntQueue.isEmpty());
assertTrue(myIntQueue.size() == 0);
}
/** Test a single offer followed by a single peek. */
@Test
public final void testPeek() {
myIntQueue.offer(1);
int i = myIntQueue.peek();
assertEquals("peek on queue of size 1", 1, i);
assertTrue(myIntQueue.size() == 1);
}
/**
* Test a single offer followed by a single poll.
*/
@Test
public final void testPoll() {
myIntQueue.offer(1);
int i = myIntQueue.poll();
assertEquals("poll on queue of size 1", 1, i);
assertTrue("Wrong size after poll", myIntQueue.size() == 0);
}
/**
* Test peek of empty queue.
*/
@Test
public final void testPeekOfEmpty() {
assertEquals("Front of empty queue not null", null, myIntQueue.peek());
}
/**
* Test poll of empty queue.
*/
@Test
public final void testPollOfEmpty() {
assertEquals("Poll of empty queue should return null", null, myIntQueue
.poll());
}
/**
* Test that implementation works for a queue of strings.
*/
@Test
public final void testStringQueue() {
myStringQueue.offer("First");
myStringQueue.offer("Second");
myStringQueue.offer("Third");
assertTrue("Wrong size of queue", myStringQueue.size() == 3);
assertEquals("peek on queue of strings", "First", myStringQueue.peek());
assertEquals("String First expected", "First", myStringQueue.poll());
assertEquals("String Second expected", "Second", myStringQueue.poll());
assertEquals("String Third expected", "Third", myStringQueue.poll());
assertTrue("Queue of strings should be empty", myStringQueue.isEmpty());
}
/**
* Test that polling gives elements in right order.
*/
@Test
public final void testOrder() {
myIntQueue.offer(1);
myIntQueue.offer(2);
myIntQueue.offer(3);
myIntQueue.offer(4);
myIntQueue.offer(5);
for (int i = 1; i <= 5; i++) {
int k = myIntQueue.poll();
assertEquals("poll returns incorrect element", i, k);
}
assertTrue("Queue not empty", myIntQueue.isEmpty());
}
/**
* Test that polling all elements gives an empty queue.
*/
@Test
public final void testMakeQueueEmpty() {
myIntQueue.offer(1);
myIntQueue.offer(2);
myIntQueue.poll();
myIntQueue.poll();
assertTrue("Wrong size after poll", myIntQueue.size() == 0);
assertTrue("Queue not empty after poll", myIntQueue.isEmpty());
myIntQueue.offer(3);
myIntQueue.offer(4);
assertTrue("Wrong size after offer", myIntQueue.size() == 2);
for (int i = 3; i <= 4; i++) {
int k = myIntQueue.poll();
assertEquals("poll returns incorrect element", i, k);
}
assertTrue("Wrong size after poll", myIntQueue.size() == 0);
assertTrue("Queue not empty after poll", myIntQueue.isEmpty());
}
}
public FifoQueue() {
queue = new FifoQueue<E>();
}