私は宣言された配列のリストを持っています。国ごとにどのように値を取得し、結果を1つずつ表示するのですか?arraylistから値を取得する方法と、選択した国の選択に従って表示する方法はありますか?
例:国の検索の場合、オプション1:米国を選択する場合。それは私のarraylistからアメリカのすべての結果を一つ一つ取り出すでしょう。それ、どうやったら出来るの ?
public class Assignment {
//Declare variables
public static String[] country;
public static String[] description;
public static String[] travelmonth;
public static double[] price;
public static String[] travelpackage;
public static String[] newls;
public static void main(String[] args) {
country = new String[8];
country[0] = "Australia";
country[1] = "Australia";
country[2] = "Australia";
country[3] = "China";
country[4] = "China";
country[5] = "USA";
country[6] = "USA";
country[7] = "USA";
travelmonth = new String[8];
travelmonth[0] = "June";
travelmonth[1] = "June";
travelmonth[2] = "July";
travelmonth[3] = "August";
travelmonth[4] = "September";
travelmonth[5] = "October";
travelmonth[6] = "October";
travelmonth[7] = "December";
description = new String[8];
description[0] = "6D5N Gold Coast Adventure";
description[1] = "5D4N Melbourne Tour";
description[2] = "6D5N Sydney City and Wine Tour";
description[3] = "4D3N Beijing City and Great Wall Adventure";
description[4] = "5D4N Hangzhou Scenic Tour";
description[5] = "8D7N California Adventure";
description[6] = "6D5N New York Shopping Trip";
description[7] = "8D7N Los Angeles, Las Vegas Winter Wonderland";
price = new double[8];
price[0] = 1600;
price[1] = 1405.5;
price[2] = 1700;
price[3] = 880;
price[4] = 990;
price[5] = 2500;
price[6] = 2106.6;
price[7] = 2650.1;
travelpackage = new String[8];
travelpackage[0] = "1 of 8";
travelpackage[1] = "2 of 8";
travelpackage[2] = "3 of 8";
travelpackage[3] = "4 of 8";
travelpackage[4] = "5 of 8";
travelpackage[5] = "6 of 8";
travelpackage[6] = "7 of 8";
travelpackage[7] = "8 of 8";
int choice = 0;
do {
String msg ="Main Menu\n";
msg += "===========\n";
msg+= "1. Display all Travel Packages\n";
msg+= "2. Random tour selection\n";
msg+= "3. Search by country\n";
msg+= "0. Quit";
//choice is an integer, therefore you need to convert
choice = Integer.parseInt(JOptionPane.showInputDialog(msg));
if(choice == 1) {
displayAllPackages();
}
if(choice == 2) {
JOptionPane.showMessageDialog(null, "Are you ready for your next holiday?");
generateRandomPackage();
}
if(choice == 3) {
searchByCountry();
}
}
while(choice!=0);
JOptionPane.showMessageDialog(null, "Thank You for using the system");
}
public static void searchByCountry() {
List<String> listString = new ArrayList<String>();
listString.add("Australia");
listString.add("Australia");
listString.add("Australia");
listString.add("China");
listString.add("China");
listString.add("USA");
listString.add("USA");
listString.add("USA");
Set<String> setString = new HashSet<String>(listString);
listString.clear();
listString.addAll(setString);
String msgs = "";
int countryno = 0;
msgs = ""; //reset
msgs+= "Enter country number\n";
msgs+= "1. " +listString.get(0) + "\n";
msgs+= "2. " +listString.get(1) + "\n";
msgs+= "3. " +listString.get(2) + "\n";
countryno = Integer.parseInt(JOptionPane.showInputDialog(msgs));
if(countryno == 1) {
showAllUSA1();
}
}
public static void showAllUSA1() {
String msg = "";
int index = 0;
String inChoice ="";
msg = ""; //reset
msg+= "Search result : 1 \n";
msg+= "==============\n";
msg+= "Country : " +country + "\n";
msg+= "Month : " +travelmonth [index] + "\n";
msg+= "Description : " +description [index] + "\n";
msg+= "Price : " +price [index] + "\n";
msg+= "===============\n";
msg+= "Enter M to return to main menu\n";
for(int i=0; i< country.length; i++)
System.out.println(i + ":" +country[i]);
//inChoice = JOptionPane.showInputDialog(msg);
}
}
:
はでそれを呼び出します。 –
一般的なアイデアは、ハッシュマップ –
でPOJOクラスを作成して比較することでインデックスを見つけることです。コードを追加しようと思います。@older coderしかしエラーがあります。私はコードをどこに置くべきか分かりません – Cherlyn