gpt4 book ai didi

java - 返回 ArrayList 给出错误 : cannot find symbol

转载 作者:行者123 更新时间:2023-11-29 10:02:32 30 4
gpt4 key购买 nike

我试图返回一个 ArrayList 但在最后我得到错误:找不到符号。我正在向列表中添加一些字符串和 double 并将其返回到调用它的地方。

错误:

./Sample.java:55: error: cannot find symbol
return placeMatch;
^
symbol: variable placeMatch
location: class Sample
1 error

考虑到有关 try catch 的内容,我将声明语句移到了顶部,我得到:

./Sample.java:54: 错误:类型不兼容 返回位置匹配; ^ 必填:字符串 找到:ArrayList

实际代码:

import java.util.ArrayList;
//...other imports

public class Sample
extends UnicastRemoteObject
implements SampleInterface {



public Sample() throws RemoteException {

}

public String invert(String city, String state) throws RemoteException {
try{


ArrayList<Object> placeMatch = new ArrayList<Object>();
// Read the existing address book.
PlaceList place =
PlaceList.parseFrom(new FileInputStream("places-proto.bin"));

// Iterates though all people in the AddressBook and prints info about them.

for (Place Placeplace: place.getPlaceList()) {
//System.out.println("STATE: " + Placeplace.getState());
if(Placeplace.getName().startsWith(city)){
placeMatch.add(Placeplace.getName());
placeMatch.add(Placeplace.getState());
placeMatch.add(Placeplace.getLat());
placeMatch.add(Placeplace.getLon());
break;
}


}

}catch(Exception e){
System.out.println("opening .bin failed:" + e.getMessage());
}
return placeMatch;
}

最佳答案

您需要声明:

ArrayList<Object> placeMatch = new ArrayList<Object>();

在 try block 之外。

第二个问题:

方法返回类型是String .您不能返回 ArrayList<Object> .

解决方案取决于您需要做什么。您可以更改返回类型:

public List<Object> invert(String city, String state) throws RemoteException {

关于java - 返回 ArrayList 给出错误 : cannot find symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19709012/

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