java - 不兼容的类型:int 不能转换为 T
问题描述
我收到此错误,这是必要的代码。假设这里没有的方法正常工作。任何帮助都是极好的。(顺便说一句,我在 Ubuntu 中对文本文件进行编码)
这是错误消息:(编译后)
BinarySearchTree.java:132:错误:不兼容的类型:int 无法转换为 T insert(y);^ 其中 T 是一个类型变量: T extends Comparable<? 在 BinarySearchTree 类中声明的 super T> 注意:一些消息已被简化;使用 -Xdiags:verbose 重新编译以获得完整的输出 1 错误
详细错误消息:
BinarySearchTree.java:132:错误:没有找到适合 insert(int) insert(y) 的方法;^ 方法 BinarySearchTree.insert(T) 不适用(参数不匹配;int 无法转换为 T)方法 BinarySearchTree.insert(T,BinaryNode) 不适用(实际和正式参数列表的长度不同)其中 T 是类型-变量:T 扩展 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; // The data in the node
private BinaryNode<T> left; // Left child
private BinaryNode<T> right; // Right child
// Constructors
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; //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<T> findMin( BinaryNode<T> 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<T> *
* node to be set *
****************************************************/
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
; // 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(",") ; // Numbers are seperated by comma as instructed
for (String x : inputValues)
{
int y = Integer.parseInt(x);
insert(y);
}
}
}
解决方案
您有一个通用参数,因此在读取 CSV 时创建新树在逻辑上是正确的。
这也意味着insertList(...)
应该变成静态的,现在像
BinarySearchTree<Integer> tree = BinarySearchTree.insertList(...)
这是代码:
public static void insertList(String csv)
{
String[] inputValues = csv.split(",") ; // Numbers are seperated by comma as instructed
BinarySearchTree<Integer> tree = new BinarySearchTree<>();
for (String x : inputValues)
{
int y = Integer.parseInt(x);
tree.insert(y);
}
}
推荐阅读
- javascript - 如何在 Angular 6 中使用 angular-timelinejs3?
- c++ - 另一个类中嵌套模板的外部实例化
- amazon-web-services - AWS 参数存储中的环境变量的值以 $ 为前缀,这不是预期的
- java - 应用建议的 Bytebuddy 代理中的“重复类”错误
- javascript - 正则表达式 | 匹配带有冒号的单词但忽略类似时间的模式
- javascript - 如何使用localStorage中数组的详细信息成功登录?
- json - 如何从字节数组类中获取 JSON 数据?
- sql-server - 从 SQL 中的组合名称单元格中分离名称
- android - 约束布局问题 java.lang.AssertionError: TOP
- python - 如何从 gensim word2vec 中提取矩阵 WI 和 WO?