Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输入一个字符串,查看该字符串是否和数组中的任何字符串匹配(JAVA)_Java_Arrays_String_For Loop_Boolean - Fatal编程技术网

输入一个字符串,查看该字符串是否和数组中的任何字符串匹配(JAVA)

输入一个字符串,查看该字符串是否和数组中的任何字符串匹配(JAVA),java,arrays,string,for-loop,boolean,Java,Arrays,String,For Loop,Boolean,我创建了一个类,其目标是在该类中有一个方法,该方法接收字符串x,然后遍历一个循环,查看接收的字符串是否与字符串[]中的任何字符串匹配。这是我的密码: public class MatchCountry { public boolean findCountry(String a) { boolean match = false; String [] euCountries = {"Albania", "Andorra", "Armenia", "Austria", "Aze

我创建了一个类,其目标是在该类中有一个方法,该方法接收字符串x,然后遍历一个循环,查看接收的字符串是否与字符串[]中的任何字符串匹配。这是我的密码:

public class MatchCountry
{
  public boolean findCountry(String a)
  {
    boolean match = false;
    String [] euCountries = {"Albania", "Andorra", "Armenia", "Austria", "Azerbaijan", "Belarus", "Belgium", "Bosnia and Herzegovina",
      "Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany", "Greece",
      "Holland", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta",
      "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania","Russia","San Marino",
      "Serbia", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom", "Vatican City"};
    int l = euCountries.length;

    for (int i = 0; i < l; i++)
    {
      System.out.println(euCountries[i]);
      if (a == euCountries[i])
        match = true;
      else
        match = false;
    }
    return match;
  }

  public static void main (String args[])
  {
    MatchCountry mc = new MatchCountry();
    boolean found = mc.findCountry("Portugal");
    System.out.println(found);
  }
}
公共类匹配国家/地区
{
公共布尔findCountry(字符串a)
{
布尔匹配=假;
字符串[]欧洲国家={“阿尔巴尼亚”、“安道尔”、“亚美尼亚”、“奥地利”、“阿塞拜疆”、“白俄罗斯”、“比利时”、“波斯尼亚和黑塞哥维那”,
“保加利亚”、“克罗地亚”、“塞浦路斯”、“捷克共和国”、“丹麦”、“爱沙尼亚”、“芬兰”、“法国”、“格鲁吉亚”、“德国”、“希腊”,
“荷兰”、“冰岛”、“爱尔兰”、“意大利”、“拉脱维亚”、“列支敦士登”、“立陶宛”、“卢森堡”、“马其顿”、“马耳他”,
“摩尔多瓦”、“摩纳哥”、“黑山”、“荷兰”、“挪威”、“波兰”、“葡萄牙”、“罗马尼亚”、“俄罗斯”、“圣马力诺”,
“塞尔维亚”、“斯洛伐克”、“斯洛文尼亚”、“西班牙”、“瑞典”、“瑞士”、“土耳其”、“乌克兰”、“联合王国”、“梵蒂冈城”};
int l=euCountries.length;
对于(int i=0;i
这不管用吗?当我输出找到的布尔值时,它总是给我FALSE…

而不是这个:

  if (a == euCountries[i])
    match = true;
  else
    match = false;
这样说:

  if (a.equals(euCountries[i])) {
    match = true;
  }
您的
if
存在以下问题:

  • 当字符串不相等时,您正在执行
    match=false
    。这将重置以前找到的所有匹配项,除非您正在搜索的国家/地区是列表中的最后一个国家/地区
  • 字符串应该与
    .equals
    方法进行比较,否则您是在比较字符串引用,而不是字符串本身

Java字符串是引用类型,使用
==
比较这些引用的值。除非两个字符串是完全相同的对象,否则比较将返回false

尝试使用
euCountries[i].equals(a)
而不是
a==euCountries[i]
<代码>字符串。等于
按值而不是按引用进行比较。(由于使用
.equals
的一个陷阱,我已经颠倒了比较:如果
a
为null,那么调用
a.equals(任意)
将抛出null指针异常。反过来,您知道字符串不是null,并且
equals
将返回false。)

你可以这样说,让事情更有效率

for (int i = 0; i < l; i++) {
    if (euCountries[i].equals(a)) {
        return true;
    }
}
return false;
for(int i=0;i
通过这样做,您可以在找到匹配项后立即退出,而不是每次都在整个数组中循环。不过,无论您做什么,您都一定要摆脱
else{match=false;}
,这会给数组中的任何国家(除了最后一个国家)带来麻烦

也可以考虑使用<代码>哈什集而不是数组。通过哈希代码检查字符串是否在集合中比比较数组的每个元素要有效得多。但是,如果这样做,您可能希望它是静态的,并且在函数之外。

尝试以下方法:

for (int i = 0; i < l; i++)
{
    if (a.equals(euCountries[i])) {
        return true;
    }
}
return false;

最好使用集合实现,例如HashSet。 然而,即使有理由不使用集合,您的代码也远远不是最优的

首先,将纯常量数据放入类(静态)

这个版本效率更高,没有额外的var,因为数据是有序的,所以您知道何时停止

public boolean findCountry(String a)
{
  for (String country : euCountries) {
    int res = country.compareTo (a);  // or use compareToIgnoreCase when more appropriate
    if (res == 0) return true;
    if (res > 0) return false;
  }
  return false;
}

下一个优化步骤是构建二进制搜索

如果您的国家/地区代码数组已正确排序,为什么不使用
java.lang.Arrays.binarySearch
,这样可以省去您自己循环的麻烦,并且是标准JRE的一部分?我总是在类似的情况下使用它。

很抱歉,它确实有效。非常感谢,它不起作用是有道理的
private static final String [] euCountries = {"Albania", "Andorra", "Armenia", "Austria", "Azerbaijan", "Belarus", "Belgium", "Bosnia and Herzegovina",
  "Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany", "Greece",
  "Holland", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta",
  "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania","Russia","San Marino",
  "Serbia", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom", "Vatican City"};
public boolean findCountry(String a)
{
  for (String country : euCountries) {
    int res = country.compareTo (a);  // or use compareToIgnoreCase when more appropriate
    if (res == 0) return true;
    if (res > 0) return false;
  }
  return false;
}