类型参数T不在类型变量T(Java Generic)的范围内 [英] Type argument T is not within bounds of type-variable T(Java Generic)

查看:3732
本文介绍了类型参数T不在类型变量T(Java Generic)的范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的AVL Tee必须处理 Node< p> T>类型为T的



在这个例子中,类型是Event类。我想比较节点。 b
$ b

但是每种类型的数据都必须进行不同的比较,为此,我将比较数据传递给数据本身进行比较。



我试图让Node实现Comparable接口,并且事件类也一样。



我的代码结构如下所示:



树结构:

  public class AvlTree< T extends Comparable< T>> {

私人节点< T>根;

public AvlTree(){
root = null;
}
//在树中插入dellete操作的其他方法


$ / code $ / pre

节点结构:

  public class Node< T extends Comparable< T>>实现可比< T> {
私人T数据;
私人节点< T>剩下;
私人节点< T>对;

public Node(){
}
@Override
public int compareTo(T t){
return this.data.compareTo(t);


将放入节点Object中的事件类:

  public class Event implements Comparable< Event> {
private point point;
public Event(){
}
@Override
public int compareTo(Event t){
return 1;
}
}

给我编译错误的代码是当我'm声明一个AvlTree:

  private AvlTree< Node< Event>> treeOfEvents; 

错误:


类型参数 Node< Event> 不在类型变量T的范围内
其中T是类型变量:

T延伸在类AvlTree中声明的Comparable< T>


解决方案

Node必须像这样实现Comparable:

  public class Node< T extends Comparable< T>>实现了Comparable< Node< T>> {

,因为您的AvlTree需要一些类似的实现Comparable的泛型类型


I'm implementing a generic AVL tree but I'm having some compilation error.

My AVL Tee has to deal with Node<T> of type T.

In the example, the type is Event class.I want to compare the node.

But each type of data has to be compared differently, for that, i'm passing the comparison to the data itself to do the comparison.

I tried to make the Node implementing the Comparable Interface and the same thing to Event Class.

My code structure looks as follow:

The Tree Structure:

public class AvlTree<T extends Comparable<T>> {

private Node<T> root;

public AvlTree() {
    root = null;
}
 //other method to insert dellete operation in the tree

    }

The node Structure:

public class Node<T extends Comparable<T>> implements Comparable<T> {
        private T data;
        private Node<T> left;
        private Node<T> right;

        public Node() {
        }
         @Override
        public int compareTo(T t) {
          return this.data.compareTo(t);
        }
     }

The event Class that will be put in a node Object:

public class Event implements Comparable<Event>{
    private Point point;
    public Event() {
    }
    @Override
    public int compareTo(Event t) {
      return 1;
    }
    }

The code that gave me a compilation error is when i'm declaring an AvlTree :

private  AvlTree<Node<Event>> treeOfEvents;

Error:

type argument Node<Event> is not within bounds of type-variable T
where T is a type-variable:
T extends Comparable<T> declared in class AvlTree

解决方案

your Node has to implement Comparable like here:

public class Node<T extends Comparable<T>> implements Comparable<Node<T>> {

as your AvlTree expects something as generic Type which implements Comparable for exactly that type

这篇关于类型参数T不在类型变量T(Java Generic)的范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆