【发布时间】:2011-08-09 01:58:45
【问题描述】:
我在我的程序中使用堆栈,但是当程序尝试检查堆栈中包含的元素时出现问题。我在堆栈中使用整数数组。简短的例子是:
Stack<int[]> myStack = new Stack<int[]>();
myStack.push(new int[]{1,2});
myStack.push(new int[]{1,3});
myStack.push(new int[]{1,4});
if (myStack.contains(new int[]{1,3})) {
System.out.println("YES");
} else {
System.out.println("NO");
}
现在它被打印为“NO”。我该怎么做才能得到“是”? 我知道问题在于我没有使用相同的对象,但实际上我的程序要大得多,我不能使用它
int[] myInteger = new int[]{1,3};
myStack.push(myInteger);
myStack.contains(myInteger);
【问题讨论】:
-
Stack类(就像Vector和HashTable)只是很久以前的宿醉,恕我直言不应该再使用了。不幸的是,Collections 框架的现代部分没有堆栈,但您可以通过继承 ArrayList 并实现两个简单的方法来创建一个。