Java打印function

我需要帮助在另一个类的这个java应用程序中编写打印函数。

function是printAll我认为是正确的,其他function肯定是错误的。

public void printAll() { Iterator 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 iterator = values(); if (dictionary.getItem(SKU).getVendor() == vendor) System.out.println(tmp.toString()); } 

我将在下面写下整个函数,以找出此问题所需的部分。

 import data_structures.*; import java.util.Iterator; public class ProductLookup { DictionaryADT dictionary; private int maxSize; public ProductLookup(int maxSize, DictionaryADT 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 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 iterator = values(); if (dictionary.getItem(SKU).getVendor() == vendor) System.out.println(tmp.toString()); } // An iterator of the SKU keys. public Iterator keys() { return dictionary.keys(); } // An iterator of the StockItem values. public Iterator values() { return dictionary.values(); } } 

由于在没有实际看到DictionaryADT的情况下令人困惑,我将在此处包含它。

 package data_structures; import java.util.Iterator; import java.util.NoSuchElementException; public interface DictionaryADT { // 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 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 values(); } 

如果DictionaryADT是一个包含所有实际实现的类,那么您需要调用

我相信你在DictionaryADT里面有Map,就像是

 public Collection values() { return dictionary.values(); } 

要获取密钥,Iterator将更改为Set

 public Set keys() { return dictionary.keySet(); // return Set, Please perform all the set opetations. } 

我相信这就是你要找的东西。

谢谢,class纳特。

这些方法都没有任何意义:

 // An iterator of the SKU keys. public Iterator keys() { return new ; } // An iterator of the StockItem values. public Iterator values() { return null; } 

第一个不会编译,第二个会在调用时立即导致NPE。 现在,什么是DictionaryADT ? 它实现了Map吗? 如果是这样,它有一个keySetvalueSet方法,您应该使用它。 也许你可以用HashMap替换它。

你不需要printprintAlltoString调用,虽然我更愿意保留toString进行调试并编写一个单独的方法。 但是,为什么不能使用foreach循环,假设DictionaryADT实现了Map

 public void printAll() { for (final StockItem item: dictionary.valueSet()) { System.out.println(item); } } 

最后,在print方法中使用equals而不是== 。 你可以查找原因。