如何解决如何为我的shoppingList修复arraylist?
我对代码还很陌生,被困在修复我的部分代码上,看不到问题所在。第一个类是ADT类,第二个类是正在实现的ArrayList类。在shoppingListArrayList类中,我需要完成remove(),find()和contains()方法,以便可以测试它们的任何帮助。
package Shopping;
import DataStructures.*;
/**
*
*/
public interface ShoppingListADT {
/**
* Method to add a new entry. Only new entries can be added. Combines
* quantities if entry already exists.
*
* @param entry the entry to be added
*/
public void add(Grocery entry);
/**
* Method to remove an entry.
*
* @param ent to be removed
* @return true when entry was removed
*/
public boolean remove(Grocery ent);
/**
* Method to find an entry.
*
* @param index to find
* @return the entry if found
* @throws Exceptions.EmptyCollectionException
*/
public Grocery find(int index) throws IndexOutOfBoundsException,EmptyCollectionException;
/**
* Method to locate the index of an entry.
*
* @param ent to find the index
* @return the index of the entry
* @throws ElementNotFoundException if no entry was found
*/
public int indexOf(Grocery ent) throws ElementNotFoundException;
/**
* Method to determine whether the object contains an entry.
*
* @param ent to find
* @return true if and only if the entry is found
*/
public boolean contains(Grocery ent);
/**
* Gets the size of the collection.
*
* @return the size of the collection
*/
public int size();
/**
* Gets whether the collection is empty.
*
* @return true if and only if the collection is empty
*/
public boolean isEmpty();
/**
* Returns a string representing this object.
*
* @return a string representation of this object
*/
@Override
public String toString();
}
private ArrayList<Grocery> shoppingList;
public Grocery find(int index) throws IndexOutOfBoundsException,EmptyCollectionException {
if (this.isEmpty()) {
throw new EmptyCollectionException("ECE - find");
}
// check whether or not the input index number is legal
// for example,< 0 or falls outside of the size
for(int i = 0;i<this.size();i++)
if(index <= 0 || index > this.size() ){
throw new IndexOutOfBoundsException("ArrayList - find");
}
// return the corresponding entry in the shoppingList
// need to change the return value
return shoppingList.get(index);
}
public boolean contains(Grocery ent) {
boolean hasItem = false;
// go through the shoppingList and try to find the
// item in the list. If found,return true.
for(int i = 0; i < shoppingList.size(); i++){
if(shoppingList.contains(ent)){
hasItem = true;
}
}
return hasItem;
}
解决方法
该类的对象与其字段shoppingList之间存在一些混淆。 在find()方法中,编写时
if (this.isEmpty())
您可能要检查shoppingList是否为空。编写“ this.isEmpty()”不是指shoppingList,而是整个类实例。改写
if (shoppingList.isEmpty())
达到预期的行为。
不需要for循环。注意,变量“ i”从未在块内的语句中使用:则不需要它。 您只需要一个条件:
if(index <= 0 || index > shoppingList.size() ){
/// throw exception
}
contains()方法可以。
将来,请考虑粘贴整个文件。我们可以看到类名和结构(我希望它是某种集合,但不确定)和其他有用的东西。这将有助于给出更精确的答案。
,也许是这样吗?
class Response {
private ArrayList<Grocery> shoppingList;
public Grocery find(int index) throws IndexOutOfBoundsException,EmptyCollectionException {
if (shoppingList.isEmpty()) {
throw new EmptyCollectionException("ECE - find");
}
if(index < 0 || index > shoppingList.size() ) {
throw new IndexOutOfBoundsException("ArrayList - find");
}
return shoppingList.get(index);
}
public boolean contains(Grocery ent) {
return shoppingList.contains(ent);
}
}
还有一个杂货对象,该对象覆盖了等号和哈希码
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。