出于某种原因,当我传递我的ArrayList(这是来自Java的基本ArrayList的Lang。*)一个int变量时,而不是在变量的索引处获取该项(例如, : i = 0; arrayList.get(i);
应该是ArrayList中的第一项) - 它调用 WHOLE 列表。
以下是相关方法:
public ZipCode findZip (int zip) {
ZipCode aZip = new ZipCode(00000);
//System.out.println(zips.get(0)); here gives 99501,ANCHORAGE,AK
//System.out.println(zips.get(0).getZipCode()); here gives 99501
for(int i = 0; i < zips.size(); i++) {
if(zips.get(i).getZipCode() == zip)
aZip = zips.get(i);
//System.out.print(aZip); here gives the zip codes from the whole array
else
aZip = null;
}
return aZip;
//Therefore, aZip is ALWAYS null. Even if it exists.
}
我已经尝试了一系列故障排除来弄明白。到目前为止,我已经知道当我输入一个直整数时问题不会出现(例如: arrayList.get(1);
得到第二项正常)。遗憾的是,没有显示编译器错误。
null
。 我需要使用一个int,所以我可以增加它来运行列表,也是不幸的。
更大的程序片段:(与我之前的问题相同)
import java.util.*;
import java.io.*;
import java.lang.Math;
public class ZipCodeDatabase {
//Field
private ArrayList<ZipCode> zips;
//Constructor
public ZipCodeDatabase () {
zips = new ArrayList<ZipCode> ();
}
//Mutator Method
public void readZipCodeData(String filename) {
Scanner inFS = null;
FileInputStream fileByteStream = null;
try{
// open the File and set delimiters
fileByteStream = new FileInputStream(filename);
inFS = new Scanner(fileByteStream);
inFS.useDelimiter("[,\r\n]+");
// continue while there is more data to read
while(inFS.hasNext()) {
//read in all input
int aZip = inFS.nextInt();
String aCity = inFS.next();
String aState = inFS.next();
double aLat = inFS.nextDouble();
double aLon = inFS.nextDouble();
//Create and add new zipcode
ZipCode newZip = new ZipCode(aZip, aCity, aState, aLat, aLon);
zips.add(newZip);
}
fileByteStream.close();
// Could not find file
}catch(FileNotFoundException error1) {
System.out.println("Failed to read the data file: " + filename);
// error while reading the file
}catch(IOException error2) {
System.out.println("Oops! Error related to: " + filename);
}
}
//Accessor Methods
public ZipCode findZip (int zip) {
ZipCode aZip = new ZipCode(00000);
for(int i = 0; i < zips.size(); i++) {
if(zips.get(i).getZipCode() == zip)
aZip = zips.get(i);
else
aZip = null;
}
return aZip;
}
这里是getZipCode()方法(它返回邮政编码int,一小部分int的一部分,包括纬度和经度;我有一个数组):
public int getZipCode () {
return zipCode;
}
答案 0 :(得分:4)
当您找到邮政编码时,您不会停止循环,因此您在循环中的下一次迭代通常会将您的返回值设置为null。将您的陈述改为:
为您的教授编辑:
ZipCode aZip = null;
for(int i = 0; i < zips.size() && aZip == null; i++) {
if(zips.get(i).getZipCode() == zip) {
aZip = zips.get(i);
}
}
if (aZip == null) {
aZip = new ZipCode(00000);
// return new ZipCode(00000); would be better
}
return aZip;
答案 1 :(得分:1)
问题在于,当您找到目标ZipCode
并且在后续迭代中将null
分配给aZip
变量时,您不会停止迭代。
要解决此问题,您可以使用条件停止迭代:
ZipCode aZip = null;
for (int i = 0; (i < zips.size()) && (aZip == null); i++) {
if (zips.get(i).getZipCode() == zip) {
aZip = zips.get(i); // When you set this, aZip will no longer be == null
}
}
答案 2 :(得分:0)
由于在找到匹配后没有停止循环,下一次迭代的结果将覆盖您的匹配,这很可能是一个不匹配的项目,因此用null
覆盖它。有几种选择:
break
出:
public ZipCode findZip (int zip) {
ZipCode aZip = null;
for(int i = 0; i < zips.size(); i++) {
if(zips.get(i).getZipCode() == zip) {
aZip = zips.get(i);
break;
}
else
aZip = null;
}
return aZip;
}
更改循环条件:
public ZipCode findZip (int zip) {
ZipCode aZip = null;
for(int i = 0; i < zips.size() && aZip==null; i++) {
if(zips.get(i).getZipCode() == zip)
aZip = zips.get(i);
else
aZip = null;
}
return aZip;
}
不要覆盖:
public ZipCode findZip (int zip) {
ZipCode aZip = null;
for(int i = 0; i < zips.size(); i++) {
if(zips.get(i).getZipCode() == zip)
aZip = zips.get(i);
else
continue; // obsolete, but you said, your prof insist on it…
}
return aZip;
}
最后一点,最干净的解决方案是在ZipCode
课程中提供正确的equals
方法。如果ZipCode
类具有正确的equals
实现,则可以将该方法实现为:
public ZipCode findZip (int zip) {
ZipCode aZip = new ZipCode(zip);
if(zips.contains(aZip))
return aZip;
else
return null;
}
或者更短:
public ZipCode findZip (int zip) {
ZipCode aZip = new ZipCode(zip);
return zips.contains(aZip)? aZip: null;
}
答案 3 :(得分:0)
谢谢大家!
虽然其他答案确实有效,但这里有一个不同的解决方案,它给出了正确的答案:
public ZipCode findZip (int zip) {
ZipCode aZip = null;
for(int i = 0; i < zips.size(); i++) {
if(zips.get(i).getZipCode() == zip) {
aZip = zips.get(i);
}
}
return aZip;
}