简体   繁体   中英

how can I search the saved number

I want to create a program to save country code and search the country code. Also, there are at most 20 counters for saving country code. I am a new java beginner. I want to know what is the correct way to write the searchCoutryCode method by using arrays to search the saved country code before?

public static void createCountryCode(String countryName, String countrycode) {  
    if (nameCounter >= 20) {                               
        System.out.println("Full");          
    } else {
        System.out.println("Saving the number of " + countryName + ":" + countryCode);           
    }   

    countryNameRec[countryNameCounter++] = countryName;                 
    countryCounterRec[countryCounter++]= countryCode;
}

public static void searchCoutryCode(String countryName) {
    for(int i = 0; i <=20; i++){
        if(countryNameRec[i].equals(countryName)){
            System.out.println("countryNameRec[i]+ " : "+ coutryCodeRec[i]");
        } else {
            System.out.println("No records"); 
        }
    }
}

Iterating in an array to find an element is not an efficient way.
Well, as you have only 20 elements, it will very probably not cause a real issue but whatever you could have more elements to handle later and this way of doing is besides verbose.

Using a binarySearch (Arrays.binarySearch()) with a sorted array or using a Map would be probably better.

Note that actually your searchCoutryCode() doesn't return nothing. A search method have to return something : the element that it founds or nothing. You could return String :

public static String searchCoutryCode(String countryName) {
   ...
}

or better Optional<String> to handle in a cleaner way the not found case :

public static Optional<String> searchCoutryCode(String countryName) {
   ...
}

I would recommend learning about Maps. They're a smarter version of arrays, and act like dictionaries with "keys" being like the words and "values" being like a definition. Your entire method can be replaced using a call to the containsValue() or containsKey() method from the java library.

However, if you want to use arrays, i would recommend looking into the binary search methods which are part of the Java Arrays library.

This code assumes country names are unique. In your code you were giving a message about, list size full but adding records anyway. For this kind of problems as recomended by others using maps more suitable.

public static HashMap<String,String> countries = new HashMap<>();

public static void createCountryCode(String countryName, String countrycode) {
    if (countries.size() >= 20) {
        System.out.println("Full");
    } else {
        System.out.println("Saving the number of " + countryName + ":" + countrycode);
        countries.put(countryName,countrycode);
    }
}

public static void searchCoutryCode(String countryName) {
    String countryCode = countries.get(countryName);
    if(countryCode == null){
        System.out.println("No records");
    }

    else{
        System.out.println("countryName+ " : "+ countryCode");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM