java读取对象失败_java-获取错误:无法比较的类型-对象和整数

本文介绍了在Java编程中遇到的一个错误:无法比较的类型:Object和int。当尝试将对象与整数进行比较时,出现此警告。问题出现在`BinarySearchTree`类的`insert`方法中,代码试图用`==`操作符比较`Node.info`(Object类型)和`int`。为了解决这个问题,可以使用`equals()`方法代替`==`来比较对象。此外,代码展示了二叉搜索树的相关操作,包括插入、遍历和平衡等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的代码在下面,但是当我构建项目时,Netbeat发出此警告:

Error: incomparable types: Object and 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("

Calculate 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 < (int) f.info) {

f.left = new Node(x);

} else {

f.right = new Node(x);

}

}

public void visitLevel(Node x) {

System.out.print("(" + x.info + "," + x.level + "), ");

}

public void visit(Node x) {

System.out.print(x.info + ", ");

}

public void visitheight(Node x) {

System.out.print("(" + x.info + "," + x.height + "), ");

}

public void visitbafator(Node x) {

System.out.print("(" + x.info + "," + x.baFactor + "), ");

}

public void breadth_first_traverse() {

MyQueue queue = new MyQueue();

Node p = root;

if (p != null) {

queue.enqueue(p);

}

while (!queue.isEmpty()) {

Node q = (Node) queue.dequeue();

visitLevel(q);

if (q.left != null) {

queue.enqueue(q.left);

}

if (q.right != null) {

queue.enqueue(q.right);

}

}

}

public void breadth_first_traverse1() {

MyQueue queue = new MyQueue();

Node p = root;

if (p != null) {

queue.enqueue(p);

}

while (!queue.isEmpty()) {

Node q = (Node) queue.dequeue();

visitheight(q);

if (q.left != null) {

queue.enqueue(q.left);

}

if (q.right != null) {

queue.enqueue(q.right);

}

}

System.out.println("");

}

public void breadth_first_traverse2() {

MyQueue queue = new MyQueue();

Node p = root;

if (p != null) {

queue.enqueue(p);

}

while (!queue.isEmpty()) {

Node q = (Node) queue.dequeue();

visitbafator(q);

if (q.left != null) {

queue.enqueue(q.left);

}

if (q.right != null) {

queue.enqueue(q.right);

}

}

System.out.println("");

}

public void breadth_first_traverse3() {

MyQueue queue = new MyQueue();

Node p = root;

if (p != null) {

queue.enqueue(p);

}

while (!queue.isEmpty()) {

Node q = (Node) queue.dequeue();

visit(q);

if (q.left != null) {

queue.enqueue(q.left);

}

if (q.right != null) {

queue.enqueue(q.right);

}

}

}

public void inorder(Node x) {

if (x == null) {

return;

}

inorder(x.left);

visit(x);

inorder(x.right);

}

public void calLevel(Node n, int i) {

if (n.left == null && n.right == null) {

n.level = i;

} else {

n.level = i;

if (n.left != null) {

calLevel(n.left, i + 1);

}

if (n.right != null) {

calLevel(n.right, i + 1);

}

}

}

public int treeheight(Node p) {

if (p == null) {

return 0;

}

int left = treeheight(p.left);

int right = treeheight(p.right);

return 1 + Math.max(left, right);

}

public void calheight(Node n) {

if (n.left == null && n.right == null) {

n.height = 1;

} else {

n.height = treeheight(n);

if (n.left != null) {

calheight(n.left);

}

if (n.right != null) {

calheight(n.right);

}

}

}

public void calbaFactor(Node n) {

if (n.left == null && n.right == null) {

n.baFactor = 0;

} else {

n.baFactor = treeheight(n.right) - treeheight(n.left);

if (n.baFactor > 1 || n.baFactor < -1) {

i++;

}

if (n.left != null) {

calbaFactor(n.left);

}

if (n.right != null) {

calbaFactor(n.right);

}

}

}

public void AVL(Node n) {

if (i > 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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值