【问题标题】:Java - An array of int that will return Boolean and the indexJava - 将返回布尔值和索引的 int 数组
【发布时间】:2014-02-18 13:21:52
【问题描述】:

java 有没有办法检查指定用户输入的整数数组并返回布尔值以及它所在的索引?我有我的办法,但我也想知道哪个索引找到了确切的整数

代码片段:

int numUser = Integer.parseInt(inputUser);
boolean ispresent = false;

for(int x = 0; x<=4; x++){
    if(sum[x] == numUser){
        ispresent = true;
    }else{
        ispresent = false;
    }
}

if(ispresent== true){
    System.out.println("The number is in the array");
}else{
    System.out.println("The number is not in the array");
}

【问题讨论】:

  • int indexboolean flag作为实例变量创建一个class的新实例并返回这个对象。
  • 请不要将变量命名为Boolean。这是非常非常糟糕的风格。
  • 表达式sum[x] == numUser 已经是布尔类型。您无需将其分配给变量。您可以简单地执行以下操作:if(sum[x] == numUser) { //如果为真,请执行您的操作} `
  • 这段代码实际上是错误的。 else { ispresent = false; } 应该被删除,因为当你找到numUser 时它可能会覆盖ispresent 的值!目前,您的代码测试numUser 是否出现在数组的最后一个位置。
  • Where is Java's Array indexOf? 的可能重复项

标签: java arrays boolean


【解决方案1】:

当它在数组中找不到它时,您可能会返回 -1。由于数组中没有-1索引,因此可以看出它没有出现在数组中。

如果该值确实出现在数组中,则可以返回该索引。因为你总是返回一个 int。

【讨论】:

    【解决方案2】:

    您的问题是非常典型的 indexOf()。通常约定在未找到元素时返回 -1,否则返回索引(大于或等于 0)。

    你有不同的方法来达到这个结果。

    将您的数组转换为列表

    int numUser = Integer.parseInt(inputUser);
    int index = Arrays.asList(sum).indexOf(numUser);
    if (index < 0) {
        System.out.println("The number is not in the array");
    } else {
        System.out.println("The number is in the array: " + index);
    }
    

    使用 Apache ArrayUtils

    int numUser = Integer.parseInt(inputUser);
    int index = ArrayUtils.indexOf(sum, numUser);
    if (index < 0) {
        System.out.println("The number is not in the array");
    } else {
        System.out.println("The number is in the array: " + index);
    }
    

    自己写

    int numUser = Integer.parseInt(inputUser);
    int index = -1;
    for (int i = 0; i < sum.length; ++i) {
        if (sum[i] == numUser) {
            index = i;
            break;
        }
    }
    if (index < 0) {
        System.out.println("The number is not in the array");
    } else {
        System.out.println("The number is in the array: " + index);
    }
    

    如果您不介意转换为List,我会使用第一种方法(代码更清晰,依赖最少)。如果您介意并且已经在您的项目中使用 Apache Utils,则第二种方法很好,并且避免转换。如果您想保持尽可能少的依赖关系并且可以处理更复杂的源代码,那么第三种方法可能就是您所追求的!

    【讨论】:

    • 谢谢!我用了第三个,效果很好。我真的需要索引来操作另一个数组:))
    • 不客气。如果您希望我进一步解释代码,请告诉我。如果您是 Java 初学者,也许一切都不像我想象的那么明显?请注意,例如,所有三种方法都会为您提供索引,并且如果您没有充分的理由避免将 Array 转换为 List,则第一个解决方案会为您提供更易于理解的代码! ;)
    【解决方案3】:

    你可以使用:

    int result = Arrays.asList(array).indexOf(value);
    boolean contained = result != -1;
    

    结果为-1 表示该值不在数组中;如果结果 >= 0,则为实际索引。

    【讨论】:

      【解决方案4】:

      使用

      int numUser = Integer.parseInt(inputUser);
      int indexOfNumUser = Arrays.asList(sum).indexOf(numUser);
      

      如果numUser 存在,则indexOfNumUser 包含数组中num 的索引。如果不是,则包含 -1。

      【讨论】:

        【解决方案5】:

        如果您没有使用它,您也可以尝试用 ArrayList 替换整数数组。 这将使您的任务更容易解决。

        【讨论】:

          【解决方案6】:

          严格来说,在“标准”Java 中,您应该使用之前发布的答案(转换为 List 并使用 indexOf)。 但如果你想坚持使用 Array,请使用 ApacheCommons ArrayUtils.indexOf(int[], int)

          来自文档:

          Returns:
          the index of the value within the array, INDEX_NOT_FOUND (-1) if not found or null array input
          

          【讨论】:

            猜你喜欢
            • 2022-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-04
            • 2018-10-10
            • 2014-12-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多