为什么在java中双if语句返回数组中的空搜索

时间:2015-10-29 21:36:09

标签: java arrays

我在java中编写了下面的代码来比较并返回数组中的searhKey,但是没有返回数组中的任何内容

package arraytest;

public class ArrayTest {

    public static void main(String[] args) {
        int objType[] = new int[10];
        int i = 0;
        int arrSize;
        int searchKey = 0;

        objType[0] = 20;
        objType[1] = 15;
        objType[2] = 10;
        objType[3] = 11;
        objType[4] = 17;
        arrSize = 5;

        for (i = 0; i < arrSize; i++) {
            System.out.println(objType[i]);
        }

        searchItem(objType, searchKey, arrSize);
    }

    public static void searchItem(int objType[], int searchKey, int arrSize) {
        int i = 0, temp = 10;
        for (int j = 0; j < arrSize; j++)
            if (objType[i] == temp) {
                searchKey = temp;
                if (j == arrSize)
                    System.out.println("Search key not found");
                System.out.println("found search key " + searchKey);
            }
    }
}

3 个答案:

答案 0 :(得分:3)

您正在j进行迭代,但使用i进行比较:

for (int j = 0; j < arrSize; j++)
            if (objType[i] == temp) {

此外,您将searchKey作为参数传递,但将其用作输出的临时变量,这没有多大意义。

确保你确实想要传递arrSize,因为你可以使用objType.length或增强for(如果你想搜索部分数据,它仍然可以,但是这个名称有误导性,但是我怀疑这是你的意图)

那可能是:

 public static void searchItem(int arr[], int searchKey) {
     int found = 0;
     for (int item : arr) {
         if (item == searchKey) {
              System.out.println("found search key " + searchKey);
              found ++;             
        }    
    }
    if (found == 0) System.out.println("Search key not found");
 }

但是,如果找到了密钥,您可能想要中断,或者返回找到的密钥的数量。这样,该方法只产生输出。

答案 1 :(得分:2)

这可能是一个合理的解决方案(感谢@Tom的评论,我测试了代码):

package arraytest;

public class ArrayTest {

    public static void main(String[] args) {
        int objType[] = new int[10];
        int i = 0;
        int arrSize;
        int searchKey = 10;

        objType[0] = 20;
        objType[1] = 15;
        objType[2] = 10;
        objType[3] = 11;
        objType[4] = 17;
        arrSize = 5;

        for (i = 0; i < arrSize; i++) {
            System.out.println(objType[i]);
        }

        searchItem(objType, searchKey, arrSize);
    }

    public static void searchItem(int objType[], int searchKey, int arrSize) {
        for (int j = 0; j < arrSize; j++) {
            if (objType[j] == searchKey) {
                System.out.println("found search key " + searchKey);
                return;
            }
        }
        System.out.println("Search key not found");
    }
}

答案 2 :(得分:2)

您的嵌套if语句永远不会被命中:

if (j == arrSize)
    System.out.println("Search key not found");

这是因为你的foor循环for (int j = 0; j < arrSize; j++)意味着如果j == arrSize循环将退出并且内部不会运行任何内容。您希望将if语句置于for循环之外,并将j更改为i

int i = 0, temp = 10;
for (i = 0; i < arrSize; i++) {
    if (objType[i] == temp) {
        searchKey = temp;
        System.out.println("found search key " + searchKey);
        break;
    }
}

if (i == arrSize) {
    System.out.println("Search key not found");
}