英文:
Java Vector Stack Contains Implementation
问题
以下是你提供的代码的翻译部分:
public boolean remove(T obj) {
if (obj == null)
return false;
while (!store.empty() && store.peek().compareTo(obj) < 0) {
temp.push(store.pop());
}
store.pop();
while (!temp.empty()) {
store.push(temp.pop());
}
size--;
return true;
}
public boolean contains(T obj) {
while (!store.empty() && store.peek().compareTo(obj) < 0) {
if (store.peek().equals(obj)) {
return true;
} else temp.push(store.pop());
}
while (!temp.empty()) {
store.push(temp.pop());
}
return false;
}
英文:
My contains and remove method is not returning the right output. This program takes a stack of names and use the contains, remove, and empty method. I wrote my methods and I'm using a driver to see if I wrote my methods correctly. I'm pretty sure that all my methods are correct besides contains and remove method. Here's my code
public boolean remove(T obj) {
if (obj == null)
return false;
while (!store.empty() && store.peek().compareTo(obj) < 0) {
temp.push(store.pop());
}
store.pop();
while(!temp.empty()) {
store.push(temp.pop());
}
size--;
return true;
}
public boolean contains(T obj) {
while (!store.empty() && store.peek().compareTo(obj) < 0) {
if (store.peek().equals(obj)) {
return true;
} else temp.push(store.pop());
}
while(!temp.empty()) {
store.push(temp.pop());
}
return false;
}
答案1
得分: 0
以下是方法remove和contains的实现:
public boolean remove(T obj) {
if (obj == null)
return false;
boolean removed = false;
while (!store.empty()) {
if(store.peek().equals(obj)) {
store.pop();
size--;
removed = true;
}else {
temp.push(store.pop());
}
}
while (!temp.empty()) {
store.push(temp.pop());
}
return removed;
}
public boolean contains(T obj) {
while (!store.empty()) {
if (store.peek().equals(obj)) {
return true;
} else
temp.push(store.pop());
}
while (!temp.empty()) {
store.push(temp.pop());
}
return false;
}
对于方法remove的实现问题在于缺少了一个判断对象相等的if语句,该语句应该用于判断是否应该移除对象。而且remove方法无论对象是否被移除,都会返回true并且减小size。
方法contains的问题在于它从未测试对象的相等性。
应用DRY(不要重复自己)原则,你应该提取代码
while (!temp.empty()) {
store.push(temp.pop());
}
到一个单独的方法中,不要一遍又一遍地重复相同的代码。类似这样:
private void pushFromTempToStore(){
while (!temp.empty()) {
store.push(temp.pop());
}
}
然后你的方法可以像这样:
public boolean add(T obj) {
if (obj == null)
return false;
while (!store.empty() && store.peek().compareTo(obj) < 0) {
temp.push(store.pop());
}
store.push(obj);
pushFromTempToStore();
size++;
return true;
}
public boolean remove(T obj) {
if (obj == null)
return false;
boolean removed = false;
while (!store.empty()) {
if(store.peek().equals(obj)) {
store.pop();
size--;
removed = true;
}else {
temp.push(store.pop());
}
}
pushFromTempToStore();
return removed;
}
public boolean contains(T obj) {
while (!store.empty()) {
if (store.peek().equals(obj)) {
return true;
} else
temp.push(store.pop());
}
pushFromTempToStore();
return false;
}
另一个重要的事情是进行测试。你应该使用一些测试框架,如JUnit或TestNG,而不是使用驱动程序。有很多关于测试的学习材料。这是一本帮助我很多的好书《Test-Driven Java Development》。它可能有点过时,所以你应该寻找一些更新的资源。你可以在这里找到这本书。
英文:
Here are implementations of the methods remove and contains:
public boolean remove(T obj) {
if (obj == null)
return false;
boolean removed = false;
while (!store.empty()) {
if(store.peek().equals(obj)) {
store.pop();
size--;
removed = true;
}else {
temp.push(store.pop());
}
}
while (!temp.empty()) {
store.push(temp.pop());
}
return removed;
}
public boolean contains(T obj) {
while (!store.empty()) {
if (store.peek().equals(obj)) {
return true;
} else
temp.push(store.pop());
}
while (!temp.empty()) {
store.push(temp.pop());
}
return false;
}
The problem with your implementation of remove was that there was no if statement that tested equality of the objects, which should be removed. The remove method also was returning true and decrementing size regardless if the object was removed or not.
The problem with contains method was that it was never testing equality of objects.
You should apply DRY(Don't Repeat Yourself) principle. Having that on mind you should extract code
while (!temp.empty()) {
store.push(temp.pop());
}
into separate method and don't repeat the same code again and again. Something like this:
private void pushFromTempToStore(){
while (!temp.empty()) {
store.push(temp.pop());
}
}
Than your methods could look like this:
public boolean add(T obj) {
if (obj == null)
return false;
while (!store.empty() && store.peek().compareTo(obj) < 0) {
temp.push(store.pop());
}
store.push(obj);
pushFromTempToStore();
size++;
return true;
}
public boolean remove(T obj) {
if (obj == null)
return false;
boolean removed = false;
while (!store.empty()) {
if(store.peek().equals(obj)) {
store.pop();
size--;
removed = true;
}else {
temp.push(store.pop());
}
}
pushFromTempToStore();
return removed;
}
public boolean contains(T obj) {
while (!store.empty()) {
if (store.peek().equals(obj)) {
return true;
} else
temp.push(store.pop());
}
pushFromTempToStore();
return false;
}
Another important thing to have in mind is testing. You should use some testing framework, like JUnit or TestNG, instead of a driver. There are plenty of materials to learn about testing. Here is a nice book that helped me a lot Test-Driven Java Development. It's a bit dated, so maybe you should try to find some newer resources...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论