2 arraylist中的Java数组
我必须编写一个程序,允许用户使用两个ArrayList(国家和首都)跟踪他访问过的所有国家及其首都。用户可以从菜单中选择三个选项,他可以:2 arraylist中的Java数组,java,arrays,loops,arraylist,Java,Arrays,Loops,Arraylist,我必须编写一个程序,允许用户使用两个ArrayList(国家和首都)跟踪他访问过的所有国家及其首都。用户可以从菜单中选择三个选项,他可以: 分别在国家/地区和资本阵列列表中添加国家/地区及其相应的资本 通过输入国家的名称,在系统中查询国家的首都。(如果访问了该国,则应显示首都,否则应向其发送错误消息:“您没有访问该国”) 退出程序 例如,arraylist国家包含[“英格兰”、“法国”、“留尼汪”、“尼泊尔”],首都包含[“伦敦”、“巴黎”、“圣丹尼斯”、“加德满都”]。如果用户访问过首都为内罗
import java.util.*;
public class MyClass {
public static void main(String[] args) {
ArrayList<String> countries = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
String country;
String capital;
String search;
countries.add("England");
countries.add("France");
countries.add("Reunion");
countries.add("Nepal");
ArrayList<String> capitals = new ArrayList<String>();
capitals.add("London");
capitals.add("Paris");
capitals.add("St Denis");
capitals.add("Kathmandu");
System.out.println("Please choose one option:");
System.out.println("1. Add a new country and its capital.");
System.out.println("2. Search for the capital of a country.");
System.out.println("3. Exit.");
int opt = sc.nextInt();
if (opt == 1) {
country = sc.nextLine();
capital = sc.nextLine();
countries.add(country);
capitals.add(capital);
} else if (opt == 2) {
System.out.println("Enter the capital of the country.");
search = sc.next();
for (int i = 0; i < countries.size(); i++) {
for (int j = 0; j < capitals.size(); j++) {
if (search.equals(capitals.get(j))) {
System.out.println("The country is " + countries.get(i));
}
}
}
} else {
System.exit(0);
}
}
}
import java.util.*;
公共类MyClass{
公共静态void main(字符串[]args){
ArrayList国家/地区=新的ArrayList();
扫描仪sc=新的扫描仪(System.in);
弦国;
字符串资本;
字符串搜索;
国家。添加(“英格兰”);
国家。添加(“法国”);
国家。添加(“团聚”);
国家。添加(“尼泊尔”);
ArrayList大写=新的ArrayList();
大写。加上(“伦敦”);
大写。加上(“巴黎”);
大写。加上(“圣丹尼斯”);
大写。加上(“加德满都”);
System.out.println(“请选择一个选项:”);
System.out.println(“1.添加一个新国家及其首都”);
System.out.println(“2.搜索一个国家的首都”);
System.out.println(“3.Exit”);
int opt=sc.nextInt();
如果(opt==1){
国家=sc.nextLine();
资本=sc.nextLine();
国家。添加(国家);
大写。加(大写);
}else if(opt==2){
System.out.println(“输入国家首都”);
search=sc.next();
对于(int i=0;i
但实际上,for
循环显然不像我进入首都时那样有效,程序就在那里终止
编辑:我不能使用HashMap,但列出了您可以使用a,国家为键
,首都为值
:
HashMap<String, String> hashMap = new HashMap<String, String>();
// add data to the HashMap
hashMap.put("England", "London"); //assign value "London" to key "England"
// get data from the HashMap
hashMap.get("England"); //returns "London"
注意:只有在列表顺序正确的情况下(第一个国家与第一个首都匹配,第二个国家与第二个首都匹配,等等),这才有效。这里有不少问题 首先:对于for循环,您的代码没有问题(与您所称的问题相关) main方法只执行一个if分支,然后终止。如果希望程序在每次完成操作后重新运行并提示菜单,直到请求终止选项,则需要将菜单包装在while循环中:
int opt= sc.nextInt();
while (opt != 3) {
if (...)
}
System.exit(0);
第二点:在Java中,通常不使用成对数组(即,两个数组通过其元素的位置进行语义链接)。您可以创建一个类:
class CountryAndCapital {
String country;
String capital;
//...
}
ArrayListy<CountryAndCapital> myArray = new ArrayList<>();
类别国家和资本{
弦国;
字符串资本;
//...
}
ArrayList myArray=新的ArrayList();
或者,正如其他人所建议的,使用地图,这是一种数据结构,将一个数据链接到另一个数据(在本例中,是首都到国家),并防止重复(从某种意义上说……):
Map countryToCapital=new HashMap();
countryToCapital.put(“法国”、“巴黎”);
//...
最后:如果您的数组是成对的,则不需要对这两个数组进行迭代!您可以迭代国家一级,然后将该索引移到首都一级:
for(int i=0; i<capitals.size();i++) {
if(search.equals(capitals.get(i))) {
System.out.println("The country is "+countries.get(i));
}
}
for(inti=0;i您的方法有一个问题:有
我认为一个适合你需要的好的数据结构应该是
Map<County,Capital[]>
Map
地图非常有用,文档非常有用
我想:如果我去了梵蒂冈城邦,我会同时在罗马,虽然不在意大利。嗯……如果梵蒂冈城有机场,那是真的,否则我肯定以前去过意大利
在循环时将代码放入
改用HashMap
在从用户获取输入之前给出消息
从控制台获取输入时,始终尝试将整行作为输入
HashMap countries=newhashmap();
扫描仪sc=新的扫描仪(System.in);
弦国;
字符串资本;
字符串搜索;
国家。put(“英国”、“伦敦”);
国家。put(“法国”、“巴黎”);
国家。put(“留尼汪”、“圣丹尼斯”);
国家。put(“尼泊尔”、“加德满都”);
while(true){
System.out.println(“请选择一个选项:”);
System.out.println(“1.添加一个新国家及其首都”);
System.out.println(“2.搜索一个国家的首都”);
System.out.println(“3.Exit”);
System.out.print(“输入您的选择:”);
int opt=Integer.parseInt(sc.nextLine());
如果(opt==1){
系统输出打印(“国家:”);
国家=sc.nextLine();
系统输出打印(“大写:”);
for(int i=0; i<capitals.size();i++) {
if(search.equals(capitals.get(i))) {
System.out.println("The country is "+countries.get(i));
}
}
Map<County,Capital[]>
HashMap<String, String> countries = new HashMap<>();
Scanner sc = new Scanner(System.in);
String country;
String capital;
String search;
countries.put("England", "London");
countries.put("France", "Paris");
countries.put("Reunion", "St Denis");
countries.put("Nepal", "Kathmandu");
while (true) {
System.out.println("Please choose one option:");
System.out.println("1. Add a new country and its capital.");
System.out.println("2. Search for the capital of a country.");
System.out.println("3. Exit.");
System.out.print("Enter your choice : ");
int opt = Integer.parseInt(sc.nextLine());
if (opt == 1) {
System.out.print("Country : ");
country = sc.nextLine();
System.out.print("Capital : ");
capital = sc.nextLine();
countries.put(country, capital);
} else if (opt == 2) {
System.out.print("Enter the capital of the country : ");
search = sc.nextLine();
for(Map.Entry<String, String> entry : countries.entrySet()){
if(search.equals(entry.getValue())){
System.out.println("The country is " + entry.getKey());
break;
}
}
} else {
System.exit(0);
}
}