Java数组-搜索数组中的元素,然后找到索引
我正试图写一个程序来搜索一个城市,给定一个邮政编码。程序必须在数组postalcode中搜索该邮政编码,并返回城市名称。 到目前为止,我掌握的代码是:Java数组-搜索数组中的元素,然后找到索引,java,arrays,Java,Arrays,我正试图写一个程序来搜索一个城市,给定一个邮政编码。程序必须在数组postalcode中搜索该邮政编码,并返回城市名称。 到目前为止,我掌握的代码是: 我遇到的问题是,我不知道如何将请求的代码链接到数组中的城市。例如,用户键入代码2000,因此是后编码(1),我想要的城市是安特卫普,因为城市[ 1 ]。 < P>我会认真考虑使用HashMap来代替2个数组。 HashMap<int,String> cities = new HashMap<int,String>();
我遇到的问题是,我不知道如何将请求的代码链接到数组中的城市。例如,用户键入代码2000,因此是后编码(1),我想要的城市是安特卫普,因为城市[ 1 ]。 < P>我会认真考虑使用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);
编辑:阵列的解决方案:
我必须说,这是一种非常奇怪的方法,但如果您真的想使用阵列:
我假设city数组的长度与postalcode数组的长度相同
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])
在第一个数组中搜索时,保存成功的索引并获取另一个数组的元素
if(postalCode[i]==input)
index=i;
现在你想要城市[索引]
索引应该在搜索循环之外声明,以便在搜索之后可以访问in,除非您以后不需要访问,因为您想提高使用数组的技能,我想这会有所帮助。没有什么复杂或有效的,但这已经足够了
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
}
}
使用两个数组确实不是实现这一点的方法,但您所拥有的似乎是城市索引中的城市与postalcode中对应的代码中的城市
您需要通过postalcode进行线性搜索,然后拉动城市:
String foundCity = null;
for (int i = 0; i < postalcode.length; i++)
{
if (postalcode[i] == code)
{
foundCity = city[i];
break;
}
}
如果foundCity不为null,则您找到了zip并拥有该城市 实际上,接受的答案将在每次查询中花费线性时间。虽然HashMap仍然是一个更好的选择,具有恒定的摊销时间,但如果您重新排列数组,以便对后代码进行排序,则可以比使用数组的线性时间做得更好。这允许您执行Ologn二进制搜索 例如: 以下是关于²的实施情况:
由于您的目标是提高您对Java中数组的了解,我相信这些都是很好的例子。如果有一张地图,您会得到更好的服务。您是否愿意接受数组以外的替代方案?在您的情况下,映射将是给定场景中的良好数据结构。我以前从未使用过映射,我正在尝试提高我对java数组的知识。所以我想用数组来做这个程序使用正确的数据结构来解决这个问题我不想用地图,我想提高我对数组的知识。所以,没有你,这对我来说将是一个挑战。@user1873613:你想用什么或学什么取决于你。至于答案,它不值得一个下载。这很有效,非常感谢。这没那么难,我自己应该能弄明白的。
if(postalCode[i]==input)
index=i;
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
}
}
String foundCity = null;
for (int i = 0; i < postalcode.length; i++)
{
if (postalcode[i] == code)
{
foundCity = city[i];
break;
}
}
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
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;
}