【问题标题】:Finding a target int value in an array [closed]在数组中查找目标 int 值 [关闭]
【发布时间】:2021-02-23 20:50:10
【问题描述】:

这是我目前所拥有的:

public class ArraySort {

    //method to search the array for an int x

    public int search(int x) {
        int[] testArray = {};
        for (int i = 0; i < testArray.length; ++i) {
            if (testArray[i] == x) {
                return (x);
            } else if (testArray[i] != x) {
                return -1;
            } else {
                return 1;
            }
        }
    }
    //main method

    public static void main(String[] args) {
        int[] testArray = {1, 2, 3};
        int x = 3;

        System.out.print(search(x));

    }
}

【问题讨论】:

  • 不要气馁。看起来搜索方法应该是 statictestArray 应该是静态成员变量或传递给 search 方法。
  • @NomadMaker 抱歉

标签: java arrays sorting search


【解决方案1】:

您不需要两个else 语句:

public int search(int x) {
    int[] testArray = {1, 2, 3, 4, 5, 6};   // put some numbers here
    for (int i = 0; i < testArray.length; i++) {
        if (testArray[i] == x) {
            return (x);
        }
    }
    return -1;
}

想一想:在原始循环的第一次迭代中,是否有任何情况下您不会遇到 return 语句? (提示:答案是否定的)。因此,您的循环将在查看第一个数字后返回。

另外,您的搜索数组是空的?

编辑:我刚刚注意到您正在尝试在 main 方法中传递数组

所以如果你想将该数组传递给搜索方法,你需要为它创建一个参数:

public int search(int x, int[] testArray) {
    for (int i = 0; i < testArray.length; i++) {
        if (testArray[i] == x) {
            return (x);
        }
    }
    return -1;
}

public static void main(String[] args) {
    int[] testArray = {1, 2, 3};
    int x = 3;
    System.out.print(search(x), testArray);  // pass the array here

}

【讨论】:

  • 这在我将搜索方法更改为静态后有效。
【解决方案2】:

首先,您需要在方法中将 testArray 作为参数传递。 如果在数组中找不到值,您可以抛出异常。 请看下面的例子:

public static int search(int x, int[] testArray) {
    for (int i = 0; i < testArray.length; ++i) {
        if (testArray[i] == x) {
            return (x);
        }
    }
    throw new RuntimeException("Element not present in array exception");
}
//main method

public static void main(String[] args) {
    int[] testArray = {1, 2, 3};
    int x = 3;

    System.out.print(search(x, testArray));

}

此外,您可以使用 java 8

    public static void main(String[] args) {
        int[] testArray = {1, 2, 3};
        int x = 3;
        System.out.println(Arrays.stream(testArray).filter(value -> value ==x).findFirst().orElseThrow(() -> new RuntimeException("Element not present in array exception")));
    }

【讨论】:

    【解决方案3】:

    取决于数组是排序的还是未排序的。

    假设数组是未排序的。我们可以在 O(n) 时间内线性搜索元素的索引。第一个建议是将数组传递给方法 search()。

    public int search(int[] testArray, int x) {
        int arrLength = testArray.length;
        int i = 0;
        
        // Traverse in the array
        while (i < arrLength) {
            if (testArray[i] == x) {
                return i;        
            } else {
                i = i + 1;
            }
        }
        return -1;
    }
    

    在main方法中执行:

    System.out.println(search(testArray, 1));
    

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多