获取错误:无法比较的类型 – 对象和int

我的代码如下,但是当我构建项目时,Netbeat给了我这个警告:

错误:无法比较的类型:Object和int if(p.info == x){

有谁知道如何解决这个错误? 谢谢。

这是错误代码:

public void insert(int x) { if (root == null) { root = new Node(x); return; } Node f, p; p = root; f = null; while (p != null) { if (p.info == x) { System.out.println(" The key " + x + " already exists, no insertion"); return; } f = p; if (x < (int) p.info) { p = p.left; } else { p = p.right; } } if (x < (int) f.info) { f.left = new Node(x); } else { f.right = new Node(x); } } 

这是我到目前为止的所有代码:

 package ass6; import java.util.ArrayList; public class Ass6 { public static void main(String[] args) { BinarySearchTree bsTree = new BinarySearchTree(); String[] c = {"C6", "C2", "C7", "C1", "C5", "C9", "C4", "C8", "C3"}; bsTree.insertMany(c); System.out.println("Calculate level of all nodes:"); bsTree.breadth_first_traverse3(); System.out.println(""); bsTree.calLevel(bsTree.root, 1); bsTree.breadth_first_traverse(); System.out.println(""); System.out.println("\nCalculate the height of the tree:"); bsTree.calheight(bsTree.root); bsTree.breadth_first_traverse1(); System.out.println(""); System.out.println("Calculate balance factor of all nodes:"); bsTree.breadth_first_traverse3(); System.out.println(""); bsTree.calbaFactor(bsTree.root); bsTree.breadth_first_traverse2(); bsTree.AVL(bsTree.root); System.out.println(""); System.out.println("Balance a binary search tree:"); bsTree.breadth_first_traverse3(); System.out.println(""); bsTree.balance(); bsTree.breadth_first_traverse3(); System.out.println(""); } } class BinarySearchTree { Node root; int height; int i; void clear() { root = null; } public BinarySearchTree() { root = null; } Node search(Node p, int x) { if (p == null) { return (null); } if ((int) p.info == x) { return (p); } if (x < (int) p.info) { return (search(p.left, x)); } else { return (search(p.right, x)); } } public void insertMany(int a[]) { for (int x : a) { insert(x); } } public void insertMany(String a[]) { for (String x : a) { insert(x); } } public void insert(String x) { if (root == null) { root = new Node(x); return; } Node f, p; p = root; f = null; while (p != null) { if (p.info == x) { System.out.println(" The key " + x + " already exists, no insertion"); return; } f = p; if (x.compareTo(p.info.toString()) < 0) { p = p.left; } else { p = p.right; } } if (x.compareTo(f.info.toString()) < 0) { f.left = new Node(x); } else { f.right = new Node(x); } } public void insert(int x) { if (root == null) { root = new Node(x); return; } Node f, p; p = root; f = null; while (p != null) { if (p.info == x) { System.out.println(" The key " + x + " already exists, no insertion"); return; } f = p; if (x < (int) p.info) { p = p.left; } else { p = p.right; } } if (x  1 || n.baFactor  0) { System.out.println("The tree is not an AVL tree ."); } else { System.out.println("The tree is an AVL tree ."); } } void inOrder(ArrayList t, Node p) { if (p == null) { return; } inOrder(t, p.left); t.add((String) p.info); inOrder(t, p.right); } void balance(ArrayList t, int i, int j) { if (i > j) { return; } int k = (i + j) / 2; String x = t.get(k); insert(x); balance(t, i, k - 1); balance(t, k + 1, j); } void balance() { ArrayList t = new ArrayList(); inOrder(t, root); int n = t.size(); clear(); balance(t, 0, n - 1); } } class Node { Object info; int level = 1; int height = 0; int baFactor = 0; Node left, right; public Node(Object inf, Node le, Node ri) { info = inf; left = le; right = ri; } public Node(Object inf) { info = inf; left = right = null; } } class MyQueue { ArrayList al = new ArrayList(); MyQueue() { } public void enqueue(Object x) { al.add(x); } public Object dequeue() { Object obj = al.get(0); al.remove(0); return obj; } public boolean isEmpty() { if (al.size() <= 0) { return true; } else { return false; } } } 

在java中,您无法将基本类型(int,float,double,byte等)与实例类型(从Object派生的所有内容然后实例化)进行比较。

使用p.info.equals(x)

这里p.info是一个Objectx是一个int因此你不能简单地与==进行比较。 但

.equals将起作用,因为价值将被比较。

  Object obj = Integer.valueOf(3); int x=3; if ( obj.equals(x) ) { // true }