2012-12-28 248 views
0

我想编写一个程序来搜索城市,给定一个邮政编码。该程序必须在数组postalcode中搜索该邮政编码并将该城市的名称返回。我到目前为止的代码是:Java数组 - 搜索数组中的元素,然后找到索引

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]。

+11

你将*更好地服务于'Map '。 – Makoto

+0

你打开数组以外的其他选择吗?在你的情况下,一个地图将是你的给定场景的良好数据结构 – KyelJmD

+0

我以前没有使用过地图,我试图提高我对java中数组的知识。所以我想用数组来做这个程序 – STheFox

回答

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

谢谢,这看起来不错! – STheFox

+0

这工作,非常感谢。这并不困难,我应该能够弄清楚自己的面团。 – STheFox

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]) 
+0

我不想使用地图,我想提高我对数组的知识。所以没有你,这对我来说将是一个挑战。 – STheFox

+0

@ user1873613:它取决于你想要使用或学习。至于答案它不值得downvote –

+0

我更新了我的答案.. btw HoGent或UGent? – Thousand

1

当你第一阵列中搜索保存的成功指标,并获得其他数组的该元素。

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

,现在你想city[index]

index应搜索的循环之外声明,使得可以在搜索之后访问(除非你将不再需要以后的访问)

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 
     } 
    } 
1

使用两个数组真的是不是这样做的方法,但它似乎你有什么是postalcode

city相同的索引对应的代码在您需要通过postalcode做线性搜索,然后拉市:

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

如果foundCity不为空,你发现了拉链和有城市。

2

其实,接受的答案每个查询需要线性时间。虽然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数组的知识,我相信这些都是很好的例子。