[英]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.