gpt4 book ai didi

java - 搜索未返回对象

转载 作者:行者123 更新时间:2023-12-02 06:35:09 24 4
gpt4 key购买 nike

作为我 Java 研究的一部分,我正在尝试创建一个搜索函数,它接受一个参数 String keydestination 相关对象的 Plane .

在此之前我创建了一个 Queue 20 个对象,每个对象都有不同的参数,现在正在尝试按 destination 进行搜索.

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

    public Plane searchDestination(String key)
{
Plane current = first;
while(current.destination != key)
{
if(current.next == null)
return null;
else
current = current.next;
}
return current;
}

它返回成功的构建,但不返回具有匹配条件的对象,我尝试添加 System.out.println(current);之后return current;但编译器会抛出错误。我还有一个.displayPlane();应该显示所有平面详细信息的函数(并且在其他情况下有效),但是当我添加 current.displayPlane(); 时在我的return current;之后我收到一条错误消息,指出它无法访问。

我的搜索是否正确,或者我是否遗漏了某些内容?

最佳答案

替换

while(current.destination != key)

while(!current.destination.equals(key))

对于任何对象(如String)比较,您应该始终使用equals。不要使用 ==!= 因为这仅比较对象的引用。对于原始类型数据(如 int),您可以使用 ==!=

关于java - 搜索未返回对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19740059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com