我需要帮助在另一个类的这个java应用程序中编写打印函数。
函数是printAll我认为是对的,另一个函数肯定是错误的。
public void printAll() {
Iterator<StockItem> iterator = values();
while (iterator.hasNext())
System.out.println(iterator.next().toString());
}
// Prints a directory of all StockItems from the given vendor,
// in sorted order (ordered by SKU).
public void print(String vendor) {
Iterator<StockItem> iterator = values();
if (dictionary.getItem(SKU).getVendor() == vendor)
System.out.println(tmp.toString());
}
我将在下面写下整个函数,以了解此问题所需的部分。
import data_structures.*;
import java.util.Iterator;
public class ProductLookup {
DictionaryADT<String,StockItem> dictionary;
private int maxSize;
public ProductLookup(int maxSize, DictionaryADT<String,StockItem> dictionary) {
this(maxSize);
this.dictionary = dictionary;
}
// Constructor. There is no argument-less constructor, or default size
public ProductLookup(int maxSize) {
this.maxSize = maxSize;
}
// Adds a new StockItem to the dictionary
public void addItem(String SKU, StockItem item) {
dictionary.insert(SKU,item);
}
// Returns the StockItem associated with the given SKU, if it is
// in the ProductLookup, null if it is not.
public StockItem getItem(String SKU) {
if (SKU == null)
return null;
return dictionary.getValue(SKU);
}
// Returns the retail price associated with the given SKU value.
// -.01 if the item is not in the dictionary
public float getRetail(String SKU) {
if (!dictionary.contains(SKU))
return (float) -.01;
return getItem(SKU).getRetail();
}
public float getCost(String SKU) {
if (!dictionary.contains(SKU))
return (float) -.01;
return getItem(SKU).getCost();
}
// Returns the description of the item, null if not in the dictionary.
public String getDescription(String SKU) {
if (!dictionary.contains(SKU))
return null;
return getItem(SKU).getDescription();
}
// Deletes the StockItem associated with the SKU if it is
// in the ProductLookup. Returns true if it was found and
// deleted, otherwise false.
public boolean deleteItem(String SKU) {
if (SKU == null)
return false;
return dictionary.remove(SKU);
}
// Prints a directory of all StockItems with their associated
// price, in sorted order (ordered by SKU).
public void printAll() {
Iterator<StockItem> iterator = values();
while (iterator.hasNext())
System.out.println(iterator.next().toString());
}
// Prints a directory of all StockItems from the given vendor,
// in sorted order (ordered by SKU).
public void print(String vendor) {
Iterator<StockItem> iterator = values();
if (dictionary.getItem(SKU).getVendor() == vendor)
System.out.println(tmp.toString());
}
// An iterator of the SKU keys.
public Iterator<String> keys() {
return dictionary.keys();
}
// An iterator of the StockItem values.
public Iterator<StockItem> values() {
return dictionary.values();
}
}
由于在没有实际看到DictionaryADT的情况下令人困惑,我将在此处加入。
package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public interface DictionaryADT<K,V> {
// Returns true if the dictionary has an object identified by
// key in it, otherwise false.
public boolean contains(K key);
// Adds the given key/value pair to the dictionary. Returns
// false if the dictionary is full, or if the key is a duplicate.
// Returns true if addition succeeded.
public boolean insert(K key, V value);
// Deletes the key/value pair identified by the key parameter.
// Returns true if the key/value pair was found and removed,
// otherwise false.
public boolean remove(K key);
// Returns the value associated with the parameter key. Returns
// null if the key is not found or the dictionary is empty.
public V getValue(K key);
// Returns the key associated with the parameter value. Returns
// null if the value is not found in the dictionary. If more
// than one key exists that matches the given value, returns the
// first one found.
public K getKey(V value);
// Returns the number of key/value pairs currently stored
// in the dictionary
public int size();
// Returns true if the dictionary is at max capacity
public boolean isFull();
// Returns true if the dictionary is empty
public boolean isEmpty();
// Returns the Dictionary object to an empty state.
public void clear();
// Returns an Iterator of the keys in the dictionary, in ascending
// sorted order. The iterator must be fail-fast.
public Iterator<K> keys();
// Returns an Iterator of the values in the dictionary. The
// order of the values must match the order of the keys.
// The iterator must be fail-fast.
public Iterator<V> values();
}
答案 0 :(得分:0)
这些方法都没有任何意义:
// An iterator of the SKU keys.
public Iterator<String> keys() {
return new ;
}
// An iterator of the StockItem values.
public Iterator<StockItem> values() {
return null;
}
第一个不会编译,第二个会在调用时立即导致NPE。现在,什么是DictionaryADT
?它是否实施Map
?如果是这样,它应该使用keySet
和valueSet
方法。也许您可以将其替换为HashMap
。
您不需要toString
和print
中的printAll
来电,但我更愿意保留toString
进行调试并编写单独的方法。但是,假设DictionaryADT
实现了Map
,为什么不能使用foreach循环:
public void printAll() {
for (final StockItem item: dictionary.valueSet()) {
System.out.println(item);
}
}
最后,在equals
方法中使用==
代替print
。你可以查找原因。
答案 1 :(得分:0)
如果DictionaryADT是一个包含所有实际实现的类,那么您需要调用
我相信你在DictionaryADT里面有Map,比如
public Collection<StockItem> values() {
return dictionary.values();
}
获取密钥,Iterator更改为Set
public Set<String> keys() {
return dictionary.keySet(); // return Set, Please perform all the set opetations.
}
我相信你正在寻找的东西。
谢谢, 贝内特。