Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble swapping values as keys in generic java BST class
    primarykey
    data
    text
    <p>I was given a generic binary search tree class with the following declaration:</p> <pre><code>public class BST&lt;K extends Comparable&lt;K&gt;, V&gt; </code></pre> <p>I was asked to write a method that reverses the BST such that the values become the keys and keys become values. When I call the following method (defined in the class given)</p> <pre><code>reverseDict.put(originalDict.get(key), key); </code></pre> <p>I get the following two error messages from Netbeans:</p> <blockquote> <p>Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: BST.put</p> </blockquote> <p>And also:</p> <blockquote> <p>no suitable method found for <code>put(V,K)</code> method <code>BST.put(BST&lt;K,V&gt;.Node,K,V)</code> is not applicable (actual and formal argument lists differ in length) method <code>BST.put(K,V)</code> is not applicable (actual argument <code>V</code> cannot be converted to <code>K</code> by method invocation conversion) where <code>V</code>,<code>K</code> are type-variables:</p> <p><code>V extends Object</code> declared in method <code>&lt;K,V&gt;reverseBST(BST&lt;K,V&gt;)</code></p> <p><code>K extends Comparable&lt;K&gt;</code> declared in method <code>&lt;K,V&gt;reverseBST(BST&lt;K,V&gt;)</code></p> </blockquote> <p>From what the error messages are telling me, since my values do not extend <code>Comparable</code> I am unable to use them as keys. If I am right, how can I get around that without changing the class given (maybe a cast)? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload