简体   繁体   English

我如何搜索保存的号码

[英]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. 另外,最多有20个计数器用于保存国家/地区代码。 I am a new java beginner. 我是Java新手。 I want to know what is the correct way to write the searchCoutryCode method by using arrays to search the saved country code before? 我想知道以前通过使用数组搜索保存的国家代码来编写searchCoutryCode方法的正确方法是什么?

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. 好吧,由于您只有20个元素,因此它很可能不会引起真正的问题,但是您以后可以处理的元素更多,而这种处理方式很冗长。

Using a binarySearch (Arrays.binarySearch()) with a sorted array or using a Map would be probably better. binarySearch (Arrays.binarySearch())与已排序的数组一起使用或使用Map可能会更好。

Note that actually your searchCoutryCode() doesn't return nothing. 请注意,实际上您的searchCoutryCode()不会返回任何内容。 A search method have to return something : the element that it founds or nothing. 搜索方法必须返回某些内容:它找到的元素或什么都不返回。 You could return String : 您可以返回String

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

or better Optional<String> to handle in a cleaner way the not found case : 或更好的Optional<String>以更干净的方式处理未发现的情况:

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. 您可以通过调用Java库中的containsValue()或containsKey()方法来替换整个方法。

However, if you want to use arrays, i would recommend looking into the binary search methods which are part of the Java Arrays library. 但是,如果要使用数组,我建议您查看Java数组库中的二进制搜索方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取ICompilationUnit的保存版本? - How can I get the saved version of an ICompilationUnit? 如何在Java中搜索保存在我的arraylist中的lastName? - how do i search lastNames saved in my arraylist in java? 如何在切换页面时保留已保存的数据 - how I can retain the saved data when I switch pages 如何缩短我的while循环而不必搜索数字(字符串格式)1-10? - How can I shorten my while loop without having to search through number (in string format) 1-10? 如何使用 Java 以最优化的方式在大型排序文件中搜索特定数字/时间戳? - How can I search for a specific number/timestamp in a large sorted file in most optimized way using Java? 如何打开带有Android图库的应用程序中保存的图像? - How can I open an image saved within the app with the android gallery? 如何覆盖外部存储Android中的已保存图像 - how can i override the saved image in external storage android 如何打印出保存在LinkedHashMap中的数据 - how can I print out data saved in LinkedHashMap 如何解析保存在文本文件中的json响应 - How can I parse a json response saved in a text file 如何使用保存的首选项保存我的列表视图 - How can I save my listview with Saved Preferences
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM