I am working in a small task that allow the user to enter the regions of any country and store them in one array. Also, each time he enters a region, the system will ask him to enter the neighbours of that entered region and store these regions.
My idea is each time the user enters the region, the system will store it in an arrya and each time he enters a region, the system will ask him to enter the neighbours of that region before he enters the second region and so on, and store these inputs in a second array under a pointer of the related entry in the first array.
what I did is just the beginning as follow:
import java.util.Arrays;
import java.util.Scanner;
public class Te开发者_JS百科st {
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));
}
}
For example, assume we have 4 regions: a, b, c, d, and a has two neighbours: b and d. In this case when the user enters the first region which is (a), he will be asked to enter its neighbours which are b and d, then he will be able to enter the second region which is b and so on.
the problem now, How can I genereated the second array and make it linked to the first array?
Please help me
Thank you for your help. I really appreciate it.
Now I have used the Hashmap and everything is working well but I faced another problem in the retrieving specific region or neighbour. Besides that, I need to retrieve each one of them separatley in order to be able to apply four-colour theorem to each one of them, So how can I do that?
My Code was as following:
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);
}
}
I suggest you use collections such as Map<String, Set<String>> where the key is the region and the Set contains the neighboring regions. I would also maintain a Set of the regions whose neighbours haven't been entered. Note: is 'a' neighbours 'b' it should also be the other way around.
I used the arrays, but unfortunately there is only one missed thing which is the size of neighbours array will be fixed for each region and this is will not be appropriate even in our real life. For instance, if region A has two neighbours, that is not necessary that other regions have the same number of neighbours. So in this case what should I do?
Besides that, I have error in compiling my code, after entering the neighbours for the first region.
And this is my code:
import java.io.*;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
System.out.println("Please enter the number of neighbours");
Scanner kb = new Scanner(System.in);
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
try {
int size = Integer.parseInt(reader.readLine());
//SIZE is size of the neighbours array
String[][] regions = new String[5][size];
for (int i = 0; i < 5; i++) {
System.out.println("Please enter the region #"
+ (i + 1) + ": ");
String input = reader.readLine();
regions[i][0] = input; //get input for region;
for (int j = 1; j <= size; j++) {
System.out.println("Please enter the neighbour #"
+ (j) + ": ");
String n = reader.readLine();
regions[i][j] = n; //get input for neighbours
}
}
} catch (IOException ioe) {
System.out.println("An error have occured");
}
}
}
For instance, if region A has two neighbours, that is not necessary that other regions have the same number of neighbours. So in this case what should I do?
I'd recommend one of the proposed Collection
solutions, but the goal may be to understand multi-dimensional arrays. You can have a different size for each neighbor array, but you'll have to determine the size for each row inside the outer loop, as suggested below.
import java.util.Arrays;
import java.util.Scanner;
public class Test5 {
private static final int REGION_COUNT = 2;
public static void main(String[] args) {
System.out.println("Please enter the number of neighbours; "
+ "enter 'q' to quit.");
Scanner kb = new Scanner(System.in);
String[][] regions = new String[REGION_COUNT][];
for (int r = 0; r < regions.length; r++) {
System.out.print("How many heighbors for region #"
+ (r + 1) + ": ");
if (kb.hasNextInt()) {
int size = kb.nextInt();
regions[r] = new String[size];
kb.nextLine();
for (int n = 0; n < size; n++) {
System.out.print("Please enter the neighbour #"
+ (n) + ": ");
regions[r][n] = kb.nextLine();
}
} else System.exit(0);
}
for (String[] neighbors : regions) {
System.out.println(Arrays.toString(neighbors));
}
}
}
You can create a class or structure with a region and an array of its neighbouring regions.So that you can access both the region easily.
Cheers, Dwarak
This would be much easier using a combination of a Map and a Collection such as a Set:
Map<String, Set<String>> map = new HashMap<String, Set<String>>();
map.put("some region", new HashSet<String>());
map.get("some region").add("some neighbor");
map.get("some region").add("some other neighbor)";
System.out.println("some region's neighbors:");
for(String neighbor : map.get("some region")) {
System.out.println(neighbor);
}
精彩评论