不兼容的类型:int无法转换为T

huangapple go评论90阅读模式
英文:

incompatible types: int cannot be converted to T

问题

以下是错误消息和代码的翻译:

错误消息:(编译后)

BinarySearchTree.java:132: 错误:不兼容的类型:int 无法转换为 T
insert(y);
^
这里的 T 是一个类型变量:
T extends Comparable<? super T> 在类 BinarySearchTree 中声明
注意:某些消息已经简化;重新编译时请使用 -Xdiags:verbose 以获取完整输出
1 错误

详细错误消息:

BinarySearchTree.java:132: 错误:没有找到适合的方法来插入(int)
insert(y);
^
方法 BinarySearchTree.insert(T) 不适用
(参数不匹配;int 无法转换为 T)
方法 BinarySearchTree.insert(T,BinaryNode) 不适用
(实际和形式参数列表长度不同)
这里的 T 是一个类型变量:
T extends Comparable<? super T> 在类 BinarySearchTree 中声明
1 错误

这是我的代码:

import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;

public class BinarySearchTree<T extends java.lang.Comparable<? super T>>
{

    private static class BinaryNode<T>
    {
        private T element; // 节点中的数据
        private BinaryNode<T> left; // 左子节点
        private BinaryNode<T> right; // 右子节点

        // 构造函数
        BinaryNode( T theElement )
        {
            this( theElement, null, null );
        }

        BinaryNode( T theElement, BinaryNode<T> lt, BinaryNode<T> rt )
        {
            element = theElement;
            left = lt;
            right = rt;
        }
    }

    private BinaryNode<T> root; // 树的根节点

    // 构造函数
    public BinarySearchTree( )
    {
        root = null;
    }

    /***************************************************
    * 函数 isEmpty: isEmpty *
    * 检查树是否为空 *
    * 输入参数:无 *
    * 无 *
    * 输出:布尔值 *
    * 如果树为空,则为true,否则为false *
    ****************************************************/
    public boolean isEmpty( )
    {
        return root == null;
    }

    private BinaryNode<T> findMin( BinaryNode<T> t )
    {
        if( t == null )
            return null;
        else if( t.left == null )
            return t;
        return findMin( t.left );
    }

    /***************************************************
    * 函数 insert: insert *
    * 将项目插入树中 *
    * 输入参数:x *
    * x - 要添加到BST的项目 *
    * 输出:无 *
    * 无 *
    ****************************************************/
    public void insert( T x )
    {
        root = insert( x, root );
    }

    /***************************************************
    * 函数 insert: insert 辅助方法 *
    * 将项目插入树中 *
    * 输入参数:x,t *
    * x - 要添加到BST的项目,t - 要插入的子树 *
    * 输出:BinaryNode<T> *
    * 要设置的节点 *
    ****************************************************/
    public  BinaryNode<T> insert( T x, BinaryNode<T> t )
    {
        if( t == null )
            return new BinaryNode<T>( x, null, null );

        int compareResult = x.compareTo( t.element );

        if( compareResult < 0 )
        {
            t.left = insert( x, t.left );
        }
        else if( compareResult > 0 )
        {
            t.right = insert( x, t.right );
        }
        else
        ; // 重复;不做任何操作
        return t;
    }

    /***************************************************
    * 函数 insertList: 选项 1 *
    * 解析字符串并插入其中包含的值 *
    * 输入参数:csv *
    * csv - 逗号分隔的整数列表 *
    * 输出:无 *
    * 无 *
    ****************************************************/
    public void insertList(String csv)
    {
        String[] inputValues = csv.split(",") ; // 数字由逗号分隔,如指示
        for (String x : inputValues)
        {
            int y = Integer.parseInt(x);
            insert(y);
        }
    }
}
英文:

I'm getting this error, here is the necessary code. Assume the methods not here work correctly. Any help would be awesome. (I am coding on a text file in Ubuntu btw)

Here is the error message: (after compiling)

BinarySearchTree.java:132: error: incompatible types: int cannot be converted to T
insert(y);
^
where T is a type-variable:
T extends Comparable<? super T> declared in class BinarySearchTree
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

Verbose error message:

BinarySearchTree.java:132: error: no suitable method found for insert(int)
insert(y);
^
method BinarySearchTree.insert(T) is not applicable
(argument mismatch; int cannot be converted to T)
method BinarySearchTree.insert(T,BinaryNode<T>) is not applicable
(actual and formal argument lists differ in length)
where T is a type-variable:
T extends Comparable<? super T> declared in class BinarySearchTree
1 error

Here is my code:

import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
public class BinarySearchTree&lt;T extends java.lang.Comparable&lt;? super T&gt;&gt;
{
private static class BinaryNode&lt;T&gt;
{
private T element; // The data in the node
private BinaryNode&lt;T&gt; left; // Left child
private BinaryNode&lt;T&gt; right; // Right child
// Constructors
BinaryNode( T theElement )
{
this( theElement, null, null );
}
BinaryNode( T theElement, BinaryNode&lt;T&gt; lt, BinaryNode&lt;T&gt; rt )
{
element = theElement;
left = lt;
right = rt;
}
}
private BinaryNode&lt;T&gt; root; //root node of the tree
// Constructor
public BinarySearchTree( )
{
root = null;
}
/***************************************************
* FUNCTION isEmpty: isEmpty *
* checks if the tree is empty *
* INPUT PARAMETERS: none *
* none *
* OUTPUT: boolean *
* true if the tree is empty, false otherwise *
****************************************************/
public boolean isEmpty( )
{
return root == null;
}
private BinaryNode&lt;T&gt; findMin( BinaryNode&lt;T&gt; t )
{
if( t == null )
return null;
else if( t.left == null )
return t;
return findMin( t.left );
}
/***************************************************
* FUNCTION insert: insert *
* inserts an item into the tree *
* INPUT PARAMETERS: x *
* x - the item to be added to the BST *
* OUTPUT: none *
* none *
****************************************************/
public void insert( T x )
{
root = insert( x, root );
}
/***************************************************
* FUNCTION insert: insert helper method *
* inserts an item into the tree *
* INPUT PARAMETERS: x, t *
* x - the item to be added to the BST, subtree to be inserted in *
* OUTPUT: BinaryNode&lt;T&gt; *
* node to be set *
****************************************************/
public  BinaryNode&lt;T&gt; insert( T x, BinaryNode&lt;T&gt; t )
{
if( t == null )
return new BinaryNode&lt;T&gt;( x, null, null );
int compareResult = x.compareTo( t.element );
if( compareResult &lt; 0 )
{
t.left = insert( x, t.left );
}
else if( compareResult &gt; 0 )
{
t.right = insert( x, t.right );
}
else
; // Duplicate; do nothing
return t;
}
/***************************************************
* FUNCTION insertList: Option 1 *
* parses a string and insert the values contained in it *
* INPUT PARAMETERS: csv *
* csv - comma seperated list of integers *
* OUTPUT: none *
* none *
****************************************************/
public void insertList(String csv)
{
String[] inputValues = csv.split(&quot;,&quot;) ; // Numbers are seperated by comma as instructed
for (String x : inputValues)
{
int y = Integer.parseInt(x);
insert(y);
}
}
}

答案1

得分: 0

你有一个通用参数,因此在读取CSV时逻辑上应该创建一个新树。

这也意味着 insertList(...) 应该变成静态的,并且现在可以像这样调用它:

BinarySearchTree<Integer> tree = BinarySearchTree.insertList(...)```

以下是代码部分
```java
public static void insertList(String csv)
{
    String[] inputValues = csv.split(","); // 按照指示,数字由逗号分隔
    BinarySearchTree<Integer> tree = new BinarySearchTree<>();
    for (String x : inputValues)
    {
        int y = Integer.parseInt(x);
        tree.insert(y);
    }
}
英文:

You have a generic parameter so it will be logically correct for you to create a new tree when reading CSV.

This also means insertList(...) should become static and now be invoked like
BinarySearchTree&lt;Integer&gt; tree = BinarySearchTree.insertList(...)

Here is the code:

    public static void insertList(String csv)
	{
		String[] inputValues = csv.split(&quot;,&quot;) ; // Numbers are seperated by comma as instructed
		BinarySearchTree&lt;Integer&gt; tree = new BinarySearchTree&lt;&gt;();
		for (String x : inputValues)
		{
			int y = Integer.parseInt(x);
			tree.insert(y);
		}
	}

huangapple
  • 本文由 发表于 2020年9月23日 09:57:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64019951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定