【发布时间】:2015-03-13 15:17:10
【问题描述】:
我的arrayList 有问题。 这是我的数组列表代码
我知道我的 arrayList 方法存在一些问题,但我无法发现它。
数组列表的方法
public ArrayList<Tour> countryList(String country)
{
ArrayList<Tour> newList = null;
switch (country)
{
case "Indonesia":
country = "IN";
break;
case "Thailand":
country = "TH";
break;
case "Singapore":
country = "SI";
break;
case "Brunei":
country = "BR";
break;
case "Philipines":
country = "PH";
break;
case "Vietnam":
country = "VI";
break;
case "Laos":
country = "LA";
break;
case "Myammmar":
country = "MY";
break;
case "Malaysia":
country = "MS";
break;
}
if(TList.indexOf(country)!= 1)
{
return newList;
}
return newList;
}
在主类中
private static void displayAllToursCountry()
{
System.out.print("Please enter the name of the country: ");
String country = sc.next();
while (country == "")
{
System.out.print("Please enter a valid country name: ");
country = sc.next();
}
System.out.println("Country:" + country);
System.out.println(ta1.countryList(country));
}
为什么没有值出来? 我在哪里做错了?谢谢
【问题讨论】:
-
请为我们明确说明您的代码行为异常。 “没有价值出来”是什么意思?到底发生了什么?另外,不要使用
==或!=比较字符串。请改用equals(...)或equalsIgnoreCase(...)方法。了解==检查两个 objects 是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的重点。 -
while (country == "")== 不好。 How to compare Strings in Java -
确实,使用
while("".equals(country)) -
你的方法体可以替换成
return null;