比较两个通用对象,如果该通用对象是“更大”或“较小” [英] comparing two generic objects if the one is "greater" or "smaller"

查看:87
本文介绍了比较两个通用对象,如果该通用对象是“更大”或“较小”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的二叉树中,我想在一开始用一个 insert 方法,如果关键字小于当前节点的关键字,则实现一个新的左节点。那么如果已经有一个左节点,它会再次检查它。右/更大的节点插入遵循相同的逻辑。

我先使用 int 类型编写我的代码,因为在我使用之前测试我的代码更容易泛型(我的新主题)。它在使用 int 时有效,但我不确定如何使用<来比较两种泛型。或>。

  public ListCell< Type> checkKey(Type key,ListCell< Type> checkCell){
ListCell< Type> newCell = null; (key< checkCell.key&& checkCell.left!= null){
...
}
...
}

我不知道是否值得说,但我正在用自编码列表创建我的二叉树。
上面你可以看到我目前的支票,但是我现在无法比较我给定的密钥和checkCell.key,因为它们不是数字。

因此,我的一般问题是如果比较泛型中的键,如果它们比二叉树中的更小或更大而不是其他类型。



在此先感谢您需要确保您的泛型类型实现了解决方案 docs.oracle.com/javase/7/docs/api/java/lang/Comparable.htmlrel =nofollow noreferrer> Comparable 界面,以及然后使用 compareTo 方法。 Java不支持重载> 运算符(或者任何运算符重载,就此而言)。

文档 compareTo


返回一个负整数,零或正整数对象小于,等于或大于指定的对象。


假设键<必须映射到您的确切代码) / code>是您将存储在您的节点中的项目,并且 checkCell.key 是您的节点

  int compareResult = key.compareTo(checkCell.key); 
if(key <0){//它在左边}
else if(key == 0){//它是相同的}
else {//它会去在右边}

在您的 compareTo 方法你需要决定你班上的哪些领域确定它是排序的。例如,如果您有大小优先级字段,您可以执行:

  @Override public int compareTo(Type other){
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;

if(this == other)return EQUAL;

if(this.size< other.size)返回BEFORE;
else if(this.size> other.size)return AFTER;
else {//大小相等,所以测试优先级
if(this.priority< other.priority)返回BEFORE;
else if(this.priority> other.priority)返回AFTER;
}
return EQUAL;
}


I want to generate a binary tree with key - value pairs in their nodes.

In my binary tree I want to implement nodes at the beginning with an insert method, which implements a new left node if the key is smaller than the key of the current node. Then if there is already a left node it will check again for it. The same logic follows for right/greater node inserts.

I wrote my code first using the int type because it's way easier for me to test my code before I use generics (new topic for me). It worked when using int but I an unsure how to compare two generics with themselves by using "<" or ">".

public ListCell<Type> checkKey(Type key, ListCell<Type> checkCell) {
    ListCell<Type> newCell = null;
    if (key < checkCell.key && checkCell.left != null) {
        ...
    }
    ...
}

I don't know if it's worth saying but I'm creating my binary tree with a selfcoded list. Above you can see my current checks but i can't compare my given key now with checkCell.key because of them not being numbers.

So my general question is how to compare the keys in generics if they are "smaller" or "greater" than the other for my implementation in a binary tree.

Thanks in advance

解决方案

You would need to ensure that your generic type implemented the Comparable interface, and then use the compareTo method instead. Java does not support overloading the > operator (or any operator overloading, for that matter).

As per the documents, compareTo:

Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

An example (that you'll have to map on to your exact code), assuming that key is your item you will store in your node, and checkCell.key is your node

int compareResult = key.compareTo(checkCell.key);
if (key < 0) { // it goes on the left }
else if (key == 0) { // it is the same }
else { // it goes on the right }

In your compareTo method you need to decide what fields in your class determine it's "ordering". For example, if you have a size and priority field, you might do:

@Override public int compareTo(Type other) {
  final int BEFORE = -1;
  final int EQUAL = 0;
  final int AFTER = 1;

  if (this == other) return EQUAL;

  if (this.size < other.size) return BEFORE;
  else if (this.size > other.size) return AFTER;
  else { // size is equal, so test priority
    if (this.priority < other.priority) return BEFORE;
    else if (this.priority > other.priority) return AFTER;
  }
  return EQUAL;
}

这篇关于比较两个通用对象,如果该通用对象是“更大”或“较小”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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