我试图返回一个 ArrayList 但最后我得到错误:找不到符号。我将一些字符串和双精度添加到列表中,并将其返回给它的名称。
错误:
./Sample.java:55: error: cannot find symbol
return placeMatch;
^
symbol: variable placeMatch
location: class Sample
1 error
考虑到提到的关于 try catch 的内容,我将声明语句移到顶部,我得到:
./Sample.java:54: 错误:不兼容的类型返回 placeMatch;^ 必需:找到的字符串: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;
}
}