我一直在编写与基于婚礼的文本文件相关的最终学校项目之一。 我正在尝试编写一个返回婚礼对象的方法,
(注意:婚礼对象由新娘姓名、新郎姓名、婚礼日期、地点、宾客人数组成)。
在使用字符串的普通方法中。例如,我只需输入
String temp = "";
运行一个循环来遍历我的数组。 if 语句
temp = temp + arr[loop].toString();
return temp;
但是现在当我声明它时处理婚礼对象:
Wedding temp; - (As i cant initialize it as there is no brideName etc.)
run loop
if statement
temp = temp + array[loop];
return temp;
这是我得到 temp 可能尚未初始化的错误的地方。
有人可以帮忙提出如何解决这个问题的建议吗?太感谢了 这是实际方法的样子
public Wedding getWeddingsOnDay(String date, String venue)
{
Wedding temp;
for (int loop = 0; loop < counter; loop++)
{
if (wedArr[loop].getWeddingDate().equals(date) && wedArr[loop].getVenue().equals(venue))
temp = wedArr[loop];
else
temp = null;
}
return temp;
}
最佳答案
这样重写该方法。
public Wedding getWeddingAt(String date, String venue) {
for (Wedding w : wedArr)
{
if (w.getWeddingDate().equals(date) &&
w.getVenue().equals(venue)) {
return w;
}
}
return null;
}
关于java - 实例化和初始化对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52791883/