Java比较泛型类型 [英] Java comparing generic types

查看:195
本文介绍了Java比较泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我编写了一个Binary Search Tree类,它使用递归添加节点。现在我想用泛型将其泛化,以便更多地了解它们。

  public class GBinNode< T> {
T item;
GBinNode< T>剩下;
GBinNode< T>对;

public GBinNode(T newItem){
item = newItem;
left = null;
right = null;
}
public GBinNode(T it,GBinNode< T> le,GBinNode< T>){
item = it;
left = le;
right = ri;
}
public String toString(){
return item.toString()+;


$ / code $ / pre
$ b $我添加节点的函数在下面的类中

  public class GBinTree< T extends Comparable< T>> {
GBinNode< T>添加(T item,GBinNode< T> bn){
if(bn == null){
return new GBinNode< T>(item,null,null);

if(item bn.left = add(item,bn.left);
}
else {
bn.right = add(item,bn.right);
}
return bn;
}

public void toString(GBinNode< T> root){
GBinNode< T> curr = root;
if(curr == null)
return;
else {
toString(curr.left);
System.out.println(curr.toString()); //顺序遍历
toString(curr.right);




$ b $ p
$ b

主类具有以下代码。我使用字符串,但数据类型可能是一些复杂类型。

  GBinTree< String> bt = new GBinTree< String>(); 
GBinNode< String> root = null;
root = bt.add(Calex,root);
root = bt.add(Ealex,root);
root = bt.add(Balex,root);
root = bt.add(Dalex,root);
bt.toString(root);

我开始使用Comparable接口,但是如何编写CompareTo()函数?我不知道T是什么类型?我得到的错误是操作符<对参数类型T,T没有定义。

寻找解决方案时,一个答案是比较泛型类型的Java

  class Element< T extends Comparable< T>> 

我不明白它应该在哪里,以及它与实现Comparable的类不同。我知道这个类型的唯一位置在主类中,那么compareTo()应该在那里?我着眼于使GBinTree成为一个界面,但却弄糊涂这是否是正确的轨道?任何帮助将不胜感激。

解决方案

你不能在Java中重载运算符。 < 运算符仅适用于基元类型,而不适用于引用类型。由于 T 是一个表示引用类型的类型变量,所以您不能在<$ c>类型的变量中使用< $ C> T 。您必须使用

  if(item.compareTo(bn.item)< 0)

检查返回的值,然后决定用它做什么。



您不知道类型 T 会是什么类型,但您知道它是一种实现 Comparable 的类型,而因此实现了 compareTo()方法。


In Java, I wrote a Binary Search Tree class that adds nodes using recursion. Now I want to generalize it using Generics so I can learn more about them.

public class GBinNode<T> {
    T item;
    GBinNode<T> left;
    GBinNode<T> right;

public GBinNode(T newItem) {
    item = newItem;
    left = null;
    right = null;
    }
public GBinNode(T it, GBinNode<T> le, GBinNode<T> ri) {
    item = it;
    left = le;
    right = ri;
    }
public String toString() {
    return item.toString()+" ";
    }
}

My function to add nodes is in the following class

public class GBinTree<T extends Comparable <T>> {
  GBinNode<T> add(T item, GBinNode<T> bn) {
    if (bn==null) {
        return new GBinNode<T>(item, null, null);
    }
    if (item < bn.item) {        // ERROR HERE
        bn.left = add( item, bn.left);
    }
    else {
        bn.right = add( item, bn.right);
    }
    return bn;
}

public void toString(GBinNode<T> root) {
    GBinNode<T> curr = root;
    if (curr == null)
        return;
    else {
        toString(curr.left);
        System.out.println(curr.toString());    // inorder traversal
        toString(curr.right);
    }
}

The main class has the following code to kick things off. I'm using strings, but the data type could be some complex type.

GBinTree<String> bt = new GBinTree<String>();
    GBinNode<String> root = null;
    root = bt.add("Calex", root);
    root = bt.add("Ealex", root);
    root = bt.add("Balex", root);
    root = bt.add("Dalex", root);       
    bt.toString(root);

I started to use the Comparable interface but then how do I write the CompareTo() function? I don't know what type T will be? The error I got was "The operator < is undefined for the argument type(s) T, T".

Searching for a solution, one answer was Comparing generic types Java:

class Element<T extends Comparable<T>>

I don't understand where this should go, and how it's different from the class implementing Comparable. The only place I know the type is in the main class, so should the compareTo() be there? I looked at making GBinTree an interface, but got confused whether that was the right track? Any help would be appreciated.

解决方案

You cannot overload operators in Java. The < operator only applies to primitive types, not reference types. Since T is a type variable that represents a reference type, you cannot use < on variables of type T. You have to use

if (item.compareTo(bn.item) < 0) 

check the value returned and decide to do what you wish with it.

You don't know what the type T will be but you know that it will be a type that implements Comparable and therefore implements the compareTo() method.

这篇关于Java比较泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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