返回偶数整数堆栈但堆栈为空

时间:2014-02-14 16:34:02

标签: java stack

我想在原始顺序中使用原始堆栈中的偶数整数返回一个新堆栈,但是我的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 );
}
 }

5 个答案:

答案 0 :(得分:2)

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

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

Stack<Integer> a = c

不会复制对象 - 只是对象的引用。

答案 1 :(得分:1)

由于原始代码(除了长度事故)也会破坏原始堆栈并以相反的顺序复制。

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 );
}

答案 2 :(得分:0)

您的length()方法通过将所有元素从堆栈中弹出来计算元素。所以当它返回时,无论使用使用的堆栈的大小是什么,堆栈现在都是空的。

答案 3 :(得分:0)

代码

if(length(stack)==1)
    {
        return stack;
    }
getEvenNumbers中的

对于任何事情都不是必需的,但如果输入堆栈有一个条目没有做任何事情则返回

答案 4 :(得分:0)

您的length方法正在清除您的筹码。尝试将其更改为以下内容:

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;
}

虽然它仍然会清除您的堆栈,但它会保存这些值并将它们重新放回原始堆栈,然后再从length返回。