use*_*819 1 java compiler-errors return arraylist return-value
我试图返回一个ArrayList,但在最后我得到错误:找不到符号.我在列表中添加了一些字符串和双打并将其返回到所谓的字符串.
错误:
./Sample.java:55: error: cannot find symbol
return placeMatch;
^
symbol: variable placeMatch
location: class Sample
1 error
Run Code Online (Sandbox Code Playgroud)
考虑到提到的关于try catch的内容我将我的声明声明移到了顶部,我得到:
./Sample.java:54:错误:不兼容的类型返回placeMatch; ^ required:找到的字符串: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;
}
Run Code Online (Sandbox Code Playgroud)
}
你需要声明:
ArrayList<Object> placeMatch = new ArrayList<Object>();
Run Code Online (Sandbox Code Playgroud)
在try块之外.
第二个问题:
方法返回类型是String.你不能回来ArrayList<Object>.
解决方案取决于您需要做什么.您可以更改返回类型:
public List<Object> invert(String city, String state) throws RemoteException {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
912 次 |
| 最近记录: |