Java 打印功能 [英] Java Printing Functions
问题描述
我需要帮助在另一个类的这个 java 应用程序中编写打印函数.
函数和printAll我认为是对的,另一个函数肯定是错误的.
public void printAll() {迭代器迭代器 = 值();while (iterator.hasNext())System.out.println(iterator.next().toString());}//打印来自给定供应商的所有 StockItems 的目录,//按排序顺序(按 SKU 排序).公共无效打印(字符串供应商){迭代器迭代器 = 值();if (dictionary.getItem(SKU).getVendor() == vendor)System.out.println(tmp.toString());}
我将在下面写下整个函数,用于解决此问题所需的部分.
import data_structures.*;导入 java.util.Iterator;公共类产品查找{DictionaryADT字典;私人 int maxSize;公共 ProductLookup(int maxSize, DictionaryADT 字典) {这(最大尺寸);this.dictionary = 字典;}//构造函数.没有无参数构造函数或默认大小公共产品查找(int maxSize){this.maxSize = maxSize;}//向字典中添加一个新的 StockItempublic void addItem(String SKU, StockItem item) {字典.插入(SKU,项目);}//返回与给定 SKU 关联的 StockItem,如果是//在 ProductLookup 中,如果不是,则为 null.公共 StockItem getItem(字符串 SKU){如果(SKU == null)返回空;返回字典.getValue(SKU);}//返回与给定 SKU 值关联的零售价.//-.01 如果该项目不在字典中公共浮动 getRetail(字符串 SKU){if (!dictionary.contains(SKU))返回(浮动)-.01;返回 getItem(SKU).getRetail();}公共浮动 getCost(String SKU) {if (!dictionary.contains(SKU))返回(浮动)-.01;返回 getItem(SKU).getCost();}//返回项目的描述,如果字典中没有,则返回 null.公共字符串getDescription(字符串SKU){if (!dictionary.contains(SKU))返回空;返回 getItem(SKU).getDescription();}//如果是,则删除与 SKU 关联的 StockItem//在产品查找中.如果找到则返回 true 并且//删除,否则为假.公共布尔删除项目(字符串 SKU){如果(SKU == null)返回假;返回字典.删除(SKU);}//打印所有 StockItems 及其关联的目录//价格,按排序顺序(按 SKU 排序).公共无效printAll(){迭代器迭代器 = 值();while (iterator.hasNext())System.out.println(iterator.next().toString());}//打印来自给定供应商的所有 StockItems 的目录,//按排序顺序(按 SKU 排序).公共无效打印(字符串供应商){迭代器迭代器 = 值();if (dictionary.getItem(SKU).getVendor() == vendor)System.out.println(tmp.toString());}//SKU 键的迭代器.公共迭代器键(){返回dictionary.keys();}//StockItem 值的迭代器.公共迭代器值(){返回字典.值();}}
由于没有实际看到 DictionaryADT 会让人感到困惑,所以我将其包含在此处.
包data_structures;导入 java.util.Iterator;导入 java.util.NoSuchElementException;公共接口 DictionaryADT<K,V>{//如果字典有一个对象,则返回真//输入它,否则为假.公共布尔包含(K 键);//将给定的键/值对添加到字典中.退货//如果字典已满,或者键是重复的,则为 false.//如果添加成功,则返回 true.公共布尔插入(K 键,V 值);//删除由 key 参数标识的键/值对.//如果找到并删除了键/值对,则返回 true,//否则为假.公共布尔删除(K 键);//返回与参数键关联的值.退货//如果未找到键或字典为空,则为 null.public V getValue(K key);//返回与参数值关联的键.退货//如果在字典中找不到该值,则为 null.如果更多//存在一个匹配给定值的键,返回//第一个找到.公共 K getKey(V 值);//返回当前存储的键/值对的数量//在字典中公共整数大小();//如果字典处于最大容量,则返回 true公共布尔 isFull();//如果字典为空则返回真公共布尔 isEmpty();//将 Dictionary 对象返回到空状态.公共无效清除();//以升序返回字典中键的迭代器//排序顺序.迭代器必须是快速失败的.公共迭代器<K>键();//返回字典中值的迭代器.这//值的顺序必须与键的顺序匹配.//迭代器必须是快速失败的.公共迭代器值();}
如果DictionaryADT是一个有所有实际实现的类,那么你需要调用
我相信你在 DictionaryADT 里面有 Map ,比如
public Collection值(){返回字典.值();}
获取key,Iterator改为Set
public Set键(){返回字典.keySet();//返回设置,请执行所有设置操作.}
我相信这就是您正在寻找的.p>
谢谢,班纳特.
I need help writing the printing functions in this java application of another class.
The functions are with printAll I think is right and the other function is definitely wrong.
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());
}
The entire function I will write down below for the parts that are needed for this problem.
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();
}
}
Since it was confusing without actually seeing DictionaryADT I will include it here.
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();
}
If DictionaryADT is a class with all the actual implementation, then you need to call
I believe you have Map inside DictionaryADT then, something like
public Collection<StockItem> values() {
return dictionary.values();
}
to get the keys, Iterator is changed to Set
public Set<String> keys() {
return dictionary.keySet(); // return Set, Please perform all the set opetations.
}
I believe this what you are looking for.
Thanks, Bennet.
这篇关于Java 打印功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!