Java Printing Functions

前端 未结 2 1373
再見小時候
再見小時候 2021-01-27 11:07

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 definite

相关标签:
2条回答
  • 2021-01-27 11:46

    Neither of these methods make any sense:

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

    The first won't compile, and the second instantly causes NPEs when invoked. Now, what is a DictionaryADT? Does it implement Map? If so, it has a keySet and valueSet method, which you should use. Perhaps you can replace it with HashMap.

    You do not need the toString calls in print and printAll, though I would prefer to reserve toString for debugging and write a separate method. However, why can't you use a foreach loop, assuming DictionaryADT implements Map:

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

    Finally, use equals instead of == in your print method. You can look up why.

    0 讨论(0)
  • 2021-01-27 11:57

    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.

    0 讨论(0)
提交回复
热议问题