0

我正在尝试编写一个程序来搜索一个城市,给定一个邮政编码。程序必须在数组 postalcode 中搜索该邮政编码并返回城市名称。我到目前为止的代码是:

.

import javax.swing.JOptionPane;

public class Postalcode 
{

    public static void main(String[] args) 
    {
        int[] postalcode = {9300,2000,1000,9200,9000,8500,9700,2300};
        String[] city = {"Aalst","Antwerpen","Brussel","Dendermonde","Gent","Kortrijk","Oudenaarde","Turnhout"};

        int code = Integer.parseInt(JOptionPane.showInputDialog("Give a postal code")); 
    }
}

我遇到的问题是我不知道如何将被询问的代码链接到数组中的城市。例如,用户键入代码 2000,所以这是邮政编码 [1],我想要的城市是安特卫普,因为城市 [1]。

4

6 回答 6

4

我会认真考虑为此使用 HashMap 而不是 2 个数组。

HashMap<int,String> cities =  new HashMap<int,String>();
cities.put(9000,"Gent");
cities.put(9400,"Aalst"); 
String city = cities.get(9400);
System.out.println(city);

为了进一步适应您的任务:

 int code = Integer.parseInt(JOptionPane.showInputDialog("Give a postal code"));
 string city = cities.get(code);

编辑:数组的解决方案:

我必须说这是一种非常奇怪的方法,但如果你真的想用数组来做:

我假设城市数组的长度与邮政编码数组的长度相同。

   int index = 0;
   int pCode = 9300;

for (int i = 0; i < postalcode.length; i ++)
{                       
  if (pCode == postalcode[i])
     {
        index = i;
        break;
     }

 }   

System.out.println(cities[index])   
于 2012-12-28T18:34:56.277 回答
3
int code = Integer.parseInt(JOptionPane.showInputDialog("Give a postal code"));

int requiredIndex = -1;
for (int i = 0; i < postalcode.length; i++)
    if (postalcode[i] == code)
        requiredIndex = i;
if (requiredIndex == -1){
    //there is no such postal code
} else {
    //your city is
    System.out.println(city[requiredIndex]);
}
于 2012-12-28T18:36:54.297 回答
2

实际上,接受的答案将花费每个查询的线性时间。虽然 a仍然是一个更好的选择(具有恒定的摊销时间),但如果您重新排列数组以便对其进行排序,则HashMap可以比使用数组的线性时间做得更好。postalCode这允许您执行O(log(n))二进制搜索。

例子:

final int[] orderedPostCode = { 1000, 2000, 2300, 8500, 9000, 9200, 9300, 9700 };
final String[] orderedCities = { "Brussel", "Antwerpen", "Turnhout", "Kortrijk", "Gent", "Dendermonde", "Aalst", "Oudenaarde" };

final int code = Integer.parseInt(JOptionPane.showInputDialog("Give a postal code"));

final int codePos = Arrays.binarySearch(orderedPostCode, code);
if (codePos < 0) {
    JOptionPane.showMessageDialog(null, "Postal code not found", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
    JOptionPane.showMessageDialog(null, "City: " + orderedCities[codePos]);
} 

这导致了一个有趣的后续问题:如何以快速二进制搜索所需的方式对任意一组邮政编码和城市进行排序:

int[] postalCode = {9300,2000,1000,9200,9000,8500,9700,2300};
String[] city = {"Aalst","Antwerpen","Brussel","Dendermonde","Gent","Kortrijk","Oudenaarde","Turnhout"};

int[] orderedPostCode = Arrays.copyOf(postalCode, postalCode.length);
Arrays.sort(orderedPostCode);
String[] orderedCities = rearrangeCities(city, postalCode, orderedPostCode);
System.out.println(Arrays.toString(orderedPostCode));
System.out.println(Arrays.toString(orderedCities));
// Will print the arrays of the first example

这是rearrangeCities实现O(n²)

private static String[] rearrangeCities(String[] cities, int[] postalCode, int[] orderedPostCode) {
    final String[] orderedCities = new String[cities.length];
    for (int newPos = 0; newPos < orderedPostCode.length; newPos++) {
        final int curPostalCode = orderedPostCode[newPos];
        for (int oldPos = 0; oldPos < postalCode.length; oldPos++) {
            if (postalCode[oldPos] == curPostalCode) {
                orderedCities[newPos] = cities[oldPos];
                break;
            }
        }
    }
    return orderedCities;
} 

由于您的目标是提高您对 Java 数组的了解,我相信这些都是很好的示例。

于 2012-12-28T20:15:56.120 回答
1

当您在第一个数组中搜索时,保存成功的索引并获取另一个数组的该元素。

    if(postalCode[i]==input)
        index=i;

现在你想要city[index]

index应该在搜索循环之外声明,以便在搜索之后可以访问 in(除非您以后不需要访问)

于 2012-12-28T18:37:58.657 回答
1

由于您想提高数组的技能,我想这会有所帮助。没有什么复杂或高效的,但这已经足够了。

 int[] postalcode = {9300,2000,1000,9200,9000,8500,9700,2300};
     String[] city = {"Aalst","Antwerpen","Brussel","Dendermonde","Gent","Kortrijk","Oudenaarde","Turnhout"};

     int code = Integer.parseInt(JOptionPane.showInputDialog("Give a postal code")); 

     for(int i = 0; i< postalcode.length;i++){
         if(postalcode[i] == code){
          System.out.println(city[i]); 
              //or do something with the value here
         }
     }
于 2012-12-28T18:38:36.763 回答
1

使用两个数组确实不是这样做的方法,但看起来你所拥有的是与city相应代码相同索引的城市postalcode

您需要进行线性搜索,postalcode然后拉出城市:

String foundCity = null;
for (int i = 0; i < postalcode.length; i++)
{
    if (postalcode[i] == code)
    {
        foundCity = city[i]; 
        break;   
    }
}

如果foundCity不为空,则您找到了 zip 并拥有城市。

于 2012-12-28T18:41:14.143 回答