Storing inputs in two linked arrays

I work in a small task that allows the user to enter the regions of any country and store them in one array. In addition, each time he enters a region, the system will ask him to enter the neighbors of this entered region and save these regions.

My idea is that every time a user enters a region, the system will store him in arrya, and every time he enters a region, the system will ask him to enter the neighbors of this region before he enters the second region, and therefore they store these inputs are in the second array under the pointer of the corresponding entry in the first array.

what i did is just the beginning:

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, , . , separatley, . , ?

:

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


}
} 
+3
5

, Map < String, Set < String → - , Set . , . : "" "b", .

+2

, , , , , . , , , . , , ?

, .

:

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");
        }
    }
}
+1

, A , , . , , ?

Collection, . , , .

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));
        }
    }
}
+1

. , .

Cheers, Dwarak

0

, 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 neighbors:");
for(String neighbor : map.get("some region")) {
    System.out.println(neighbor);
}
0

Source: https://habr.com/ru/post/1782172/


All Articles