Noa*_*ian -2 java methods arraylist
我有一个arrayList的问题.这是我的arrayList代码
我知道我的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;
}
Run Code Online (Sandbox Code Playgroud)
在主要班级
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));
}
Run Code Online (Sandbox Code Playgroud)
为什么没有价值出来?我做错了什么地方?谢谢
你没有把任何东西放到ArrayList中.
你刚刚做的
ArrayList<Tour> newList = null;
Run Code Online (Sandbox Code Playgroud)
然后 return newList;
它甚至没有初始化.
你应该初始化它:ArrayList<Tour> newList = new ArrayList<>()
然后把一些元素放入:newList.add(myTour);