我正在执行一项小任务,允许用户输入任何国家/地区的区域并将它们存储在一个数组中。另外,每次他进入一个区域,系统都会要求他输入该区域的邻居并存储这些区域。
我的想法是每次用户进入该区域时,系统都会将其存储在一个 arrya 中,每次他进入一个区域时,系统都会要求他在进入第二个区域之前输入该区域的邻居等等on,并将这些输入存储在第一个数组中相关条目的指针下的第二个数组中。
我所做的仅仅是开始,如下所示:
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String [] regionArray = new String[5];
for (int i = 0; i < regionArray.length; i++) {
System.out.print("Please enter the region: ");
regionArray[i] = kb.next();
//kb.nextLine(); // to get and discard the nextline token
}
System.out.print("You have entered: ");
System.out.println(Arrays.toString(regionArray));
}
}
例如,假设我们有 4 个区域:a、b、c、d,并且 a 有两个邻居:b 和 d。在这种情况下,当用户进入第一个区域 (a) 时,他将被要求输入其邻居 b 和 d,然后他将能够进入第二个区域 b,依此类推。
现在的问题是,如何生成第二个数组并将其链接到第一个数组?
请帮帮我
感谢您的帮助。我真的很感激。
现在我已经使用了 Hashmap 并且一切正常,但我在检索特定区域或邻居时遇到了另一个问题。除此之外,我需要分别检索它们中的每一个,以便能够将四色定理应用于它们中的每一个,那么我该怎么做呢?
我的代码如下:
import java.util.*;
import java.util.HashMap;
public class Test5{
public static void main(String[]args){
HashMap<String, String> neighbour = new HashMap<String, String>();
Scanner kb = new Scanner(System.in);
for(;{
System.out.println ("Enter the neighbours first then the region... and when finished press q");
String n = kb.nextLine();
if (n.equalsIgnoreCase("Q"))
break;
System.out.print("region: ");
String region = kb.nextLine();
neighbour.put(region, n);
}
System.out.println(neighbour);
}
}
最佳答案
我建议您使用 Map
关于java - 将输入存储在两个相关数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4534082/