gpt4 book ai didi

java - 返回偶数堆栈,但堆栈为空

转载 作者:行者123 更新时间:2023-12-01 23:12:01 29 4
gpt4 key购买 nike

我试图以原始顺序返回一个新堆栈,其中包含原始堆栈中的偶数整数,但我的 junit 测试一直说我的堆栈为空,并且不包含正确的值。有人可以看看我的代码并解释我做错了什么吗?我不能使用静态变量、数组或 Java 集合。

我的代码

public class StackExample {
public static Stack<Integer> getEvenNumbers(Stack<Integer> stack) {
Stack<Integer> a = stack;
Stack<Integer> c = new Stack<Integer>();
System.out.println(length(stack));
if(length(stack)==1)
{
return stack;
}
while (!(a.isEmpty())) {
int num = a.pop();
if (num % 2 == 0) {
c.push(num);
} else {

}
}
System.out.println(c.isEmpty());
return c;
}

public static int length(Stack<Integer> a) {
int length = 0;
while (!(a.isEmpty())) {
a.pop();
length++;
}
return length;
}
}

提供的堆栈类

import java.util.ArrayList;
import java.util.List;

public class Stack<T> {
private List<T> array = new ArrayList<T>();

public void push(T value) {
array.add( value );
}
public T pop() {
int last = array.size() - 1;
return array.remove( last );
}
public boolean isEmpty() {
return array.isEmpty();
}
@Override
public String toString() {
return array.toString();
}
}

我的junit测试

import static org.junit.Assert.*;

import java.lang.reflect.Field;

import org.junit.Test;

public class StackExampleTest {
private class StackTest extends Stack<Integer> {
public StackTest(int[] values) {
for (int i = values.length-1; i > -1; i--) {
push( values[ i ] );
}
}
}
@Test
public void testReflection() {
Class<?> iClass = StackExample.class;
for (Field field : iClass.getDeclaredFields()) {
if (!field.isSynthetic()) {
fail( "class should not have any fields" );
}
}
}
@Test
public void testEmpty() {
int[] input = new int[]{ };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );

assertTrue( "result should be empty", result.isEmpty() );
assertTrue( "stack should be empty", stack .isEmpty() );
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void test1Odd() {
int[] input = new int[]{ 5 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );

assertTrue( "result should be empty", result.isEmpty() );

for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void test1Even() {
int[] input = new int[]{ 4 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );

for (int expected : new int[]{ 4 }) {
if (result.isEmpty())
fail( "\"result\" empty: '"+ expected +"' expected" );
else {
int actual = result.pop();
assertEquals( "incorrect result", expected, actual );
}
}
for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testNoneEven() {
int[] input = new int[]{ 9, 77, 3, 5, 11 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );

assertTrue( "result should be empty", result.isEmpty() );

for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testSomeEven() {
int[] input = new int[]{ 44, 77, 8, 3, 5, 12 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );

for (int expected : new int[]{ 44, 8, 12 }) {
if (result.isEmpty())
fail( "\"result\" empty: '"+ expected +"' expected" );
else {
int actual = result.pop();
assertEquals( "incorrect result", expected, actual );
}
}
for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
@Test
public void testAllEven() {
int[] input = new int[]{ 12, 22, 6, 14, 12 };
Stack<Integer> stack = new StackTest( input );
Stack<Integer> result = StackExample.getEvenNumbers( stack );

for (int expected : input) {
if (result.isEmpty())
fail( "\"result\" empty: '"+ expected +"' expected" );
else {
int actual = result.pop();
assertEquals( "incorrect result", expected, actual );
}
}
for (int expected : input) {
if (stack.isEmpty())
fail( "\"stack\" empty: '"+ expected +"' expected" );
else {
int actual = stack.pop();
assertEquals( "incorrect result", expected, actual );
}
}
assertTrue( "stack and result cannot be the same object", stack != result );
}
}

最佳答案

您的长度函数会从堆栈中删除(弹出)所有元素。

请注意,将对象引用分配给另一个变量,例如

Stack<Integer> a = c

不复制对象 - 仅复制对象的引用。

关于java - 返回偶数堆栈,但堆栈为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21784638/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com