gpt4 book ai didi

java - 如何从数组列表中检索值并根据所选国家/地区显示?

转载 作者:行者123 更新时间:2023-12-02 11:50:14 27 4
gpt4 key购买 nike

我声明了一个数组列表。如何根据国家/地区检索这些值并一一显示结果?

例如,对于 SearchofCountry,如果我选择选项 1:美国。它会从我的数组列表中一一检索出 USA 的所有结果。我该怎么做?

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);


}

}

最佳答案

您有一堆按索引关联的数组。与其将它们放入数组中,为什么不为每个创建一个对象呢?

public class TravelPlan {
String country;
String description;
String travelmonth;
double price;
String travelpackage;
String newls;

public TravelPlan() {
}

public TravelPlan(String country,
String description,
String travelmonth,
double price,
String travelpackage) {
this.country = country;
this.description = description;
this.travelmonth = travelmonth;
this.price = price;
this.travelpackage = travelpackage;
}
// add setters/getters here for example
String getCountry() {
return(country);
}
void setCountry(String country) {
this.country = country;
}
double getPrice() {
return(price);
}
void setPrice(double price) {
this.price = price;
}
// continue with rest of class variables here
}

然后你可以通过以下方式实例化它:

TravelPlan one = new TravelPlan ("Australia", 
"June",
"6D5N Gold Coast Adventure",
1600,
"1 of 8");

现在您可以将它们放入数组或列表中。

  java.util.List<TravelPlan> travelPlans = new java.util.ArrayList<>();
travelPlans.add(one);
// add rest here

通过这样做,所有数组现在都变成对象了,这使得搜索变得更容易。

for (TravelPlan s : travelPlans) {
if ("USA".equals(s.getCountry()) {
//we have a match for USA, do whatever ...
}
}

假设 TravelPlans 是所有可能的 TravelPlan 的列表,您可以使用以下方法按国家/地区搜索。

List<TravelPlan> getListByCountry(String searchCountry) {
List<TravelPlan> retValue = new ArrayList<>();
for (TravelPlan s: travelPlans) {
String tempCountry = s.getCountry();
if (tempCountry != null) {
if (tempCountry.equals(searchCountry)) {
retValue.add(s);
}
}
}
return(retValue)
}

通过以下方式调用:

List matches = getListByCountry("USA");

关于java - 如何从数组列表中检索值并根据所选国家/地区显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47928133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com