Java 返回偶数整数的堆栈,但堆栈为空

Java 返回偶数整数的堆栈,但堆栈为空,java,stack,Java,Stack,我打算从原始堆栈中以原始顺序返回一个带有偶数整数的新堆栈,但我的junit测试一直说我的堆栈是空的,并且不包含正确的值。有人能看看我的代码,解释一下我做错了什么吗?我不能使用静态变量、数组或Java集合 我的代码 公共类堆栈示例{ 公共静态堆栈GetEvenNumber(堆栈){ 堆栈a=堆栈; 堆栈c=新堆栈(); 系统输出打印LN(长度(堆栈)); if(长度(堆栈)==1) { 返回栈; } 而(!(a.isEmpty()){ int num=a.pop(); 如果(数值%2==0){ c

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

我的代码

公共类堆栈示例{
公共静态堆栈GetEvenNumber(堆栈){
堆栈a=堆栈;
堆栈c=新堆栈();
系统输出打印LN(长度(堆栈));
if(长度(堆栈)==1)
{
返回栈;
}
而(!(a.isEmpty()){
int num=a.pop();
如果(数值%2==0){
c、 推送(num);
}否则{
}
}
System.out.println(c.isEmpty());
返回c;
}
公共静态整数长度(堆栈a){
整数长度=0;
而(!(a.isEmpty()){
a、 pop();
长度++;
}
返回长度;
}
}
提供的堆栈类

import java.util.ArrayList;
导入java.util.List;
公共类堆栈{
私有列表数组=新的ArrayList();
公共无效推送(T值){
数组。添加(值);
}
公共广播电台{
int last=array.size()-1;
返回数组。删除(最后一个);
}
公共布尔值为空(){
返回数组.isEmpty();
}
@凌驾
公共字符串toString(){
返回array.toString();
}
}
我的junit测试

import static org.junit.Assert.*;
导入java.lang.reflect.Field;
导入org.junit.Test;
公共类StackExampleTest{
私有类StackTest扩展了堆栈{
公共堆栈测试(int[]值){
对于(int i=values.length-1;i>-1;i--){
推(值[i]);
}
}
}
@试验
公共void testReflection(){
类iClass=StackExample.Class;
for(字段:iClass.getDeclaredFields()){
如果(!field.isSynthetic()){
失败(“类不应具有任何字段”);
}
}
}
@试验
公共空间{
int[]输入=新的int[]{};
堆栈=新的堆栈测试(输入);
堆栈结果=StackExample.getEvenNumber(堆栈);
assertTrue(“结果应为空”,result.isEmpty());
assertTrue(“堆栈应为空”,stack.isEmpty());
assertTrue(“堆栈和结果不能是同一对象”,堆栈!=结果);
}
@试验
公共void test1Odd(){
int[]输入=新的int[]{5};
堆栈=新的堆栈测试(输入);
堆栈结果=StackExample.getEvenNumber(堆栈);
assertTrue(“结果应为空”,result.isEmpty());
for(预期为int:输入){
if(stack.isEmpty())
失败(“堆栈”为空:“+expected+“'expected”);
否则{
int actual=stack.pop();
assertEquals(“结果不正确”,预期、实际);
}
}
assertTrue(“堆栈和结果不能是同一对象”,堆栈!=结果);
}
@试验
public void test1Even(){
int[]输入=新的int[]{4};
堆栈=新的堆栈测试(输入);
堆栈结果=StackExample.getEvenNumber(堆栈);
for(应为int:new int[]{4}){
if(result.isEmpty())
失败(“结果”为空:“+expected+“'expected”);
否则{
int actual=result.pop();
assertEquals(“结果不正确”,预期、实际);
}
}
for(预期为int:输入){
if(stack.isEmpty())
失败(“堆栈”为空:“+expected+“'expected”);
否则{
int actual=stack.pop();
assertEquals(“结果不正确”,预期、实际);
}
}
assertTrue(“堆栈和结果不能是同一对象”,堆栈!=结果);
}
@试验
公共void testNoneEven(){
int[]输入=新的int[]{9,77,3,5,11};
堆栈=新的堆栈测试(输入);
堆栈结果=StackExample.getEvenNumber(堆栈);
assertTrue(“结果应为空”,result.isEmpty());
for(预期为int:输入){
if(stack.isEmpty())
失败(“堆栈”为空:“+expected+“'expected”);
否则{
int actual=stack.pop();
assertEquals(“结果不正确”,预期、实际);
}
}
assertTrue(“堆栈和结果不能是同一对象”,堆栈!=结果);
}
@试验
公共void testsome偶(){
int[]输入=新的int[]{44,77,8,3,5,12};
堆栈=新的堆栈测试(输入);
堆栈结果=StackExample.getEvenNumber(堆栈);
for(预期为int:newint[]{44,8,12}){
if(result.isEmpty())
失败(“结果”为空:“+expected+“'expected”);
否则{
int actual=result.pop();
assertEquals(“结果不正确”,预期、实际);
}
}
for(预期为int:输入){
if(stack.isEmpty())
失败(“堆栈”为空:“+expected+“'expected”);
否则{
int actual=stack.pop();
assertEquals(“结果不正确”,预期、实际);
}
}
assertTrue(“堆栈和结果不能是同一对象”,堆栈!=结果);
}
@试验
公共无效测试甚至(){
int[]输入=新的int[]{12,22,6,14,12};
堆栈=新的堆栈测试(输入);
堆栈结果=StackExample.getEvenNumber(堆栈);
for(预期为int:输入){
if(result.isEmpty())
失败(“结果”为空:“+expected+“'expected”);
否则{
int actual=result.pop();
assertEquals(“结果不正确”,预期、实际);
}
}
for(预期为int:输入){
if(stack.isEmpty())
失败(“堆栈”为空:“+expected+“'expected”);
否则{
int actual=stack.pop();
assertEquals(“结果不正确”,预期、实际);
}
}
assertTrue(“堆栈和结果不能是同一对象”,堆栈!=结果);
}
}
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();
}
}
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
if(length(stack)==1)
    {
        return stack;
    }
public static int length(Stack<Integer> a)
{
    Stack<Integer> store = new Stack<Integer>();
    int length = 0;
    while (!(a.isEmpty()))
    {
        store.push(a.pop());
        length++;
    }
    while (!(store.isEmpty()))
    {
        a.push(store.pop());
    }
    return length;
}
Stack<Integer> copyEven( Stack<Integer> orig ){
    Stack<Integer> copy = new Stack<Integer>();
    doCopy( orig, copy );
    return copy;
}

void doCopy( Stack<Integer> orig, Stack<Integer> copy ){
    if( orig.isEmpty() ) return;
    Integer curr = orig.pop();
    doCopy( orig, copy );
    if( curr % 2 == 0 ) copy.push( curr );
    orig.push( curr );
}