数组元素搜索

时间:2014-10-06 09:24:10

标签: java arrays

我是java新手,一直在尝试编写如何搜索多维数组。我的代码适用于找到的元素但是当我输入一个不匹配的元素时,它不打印任何东西。请告诉我我的代码有什么问题。

import java.util.Scanner;
public class ArraySearch {
public static void main (String[] args){
    Scanner input = new Scanner(System.in);

    //lets create the array
    int [] [] arrayOfInts = {{1, 2,3,4}, {5,6,7,8},{9,10,11,12}};

    //create search variables
    System.out.println("Enter the key number to search for in the array: ");
    int key = input.nextInt();
    boolean foundIt;

    //perform search using a for loop
    for (int i = 0; i <arrayOfInts.length; i++){
        for (int j = 0; j <arrayOfInts[i].length; j++){
            if (arrayOfInts[i][j] == key) {
                foundIt = true;

                if (foundIt) {
                    System.out.println("found " + key + " at row " +i+ " column " +j);

                } else {
                    System.out.println(key + "is not in the array");  
                }
            }
        }
    }
}
}

8 个答案:

答案 0 :(得分:2)

您应该将布尔值初始化为false,因为必须在使用之前初始化局部变量:

boolean foundIt = false;

否则,如果找不到该密钥,则在{if}条件下访问密钥时,foundIt将被取消初始化。

不初始化foundIt应该给你一个补充错误(The local variable foundIt may not have been initialized),但你有另一个错误隐藏了这个错误。打印输出的if语句应该在for循环之外。现在它处于找到匹配条件的范围内,因此只有在找到匹配项时才会对其进行评估。

答案 1 :(得分:1)

包围错误。 if - else语句

if (foundIt) {
    System.out.println("found " + key + " at row " +i+ " column " +j);

} else 
        {System.out.println(key + "is not in the array");  
}

在for循环的检查中。

if (arrayOfInts[i][j] == key) {

您可能希望将其放在for循环中以显示每个匹配的消息。但是你应该在for循环中的if语句中放入println消息

if (arrayOfInts[i][j] == key) {
  System.out.println("found " + key + " at row " +i+ " column " +j);

当从未找到密钥时打印另一条消息,但必须在最后完成。确保在开头初始化布尔值!

boolean foundIt = false;
...
//at the end
if(!foundIt) {
  System.out.println("found " + key + " at row " +i+ " column " +j);
}

答案 2 :(得分:1)

您可以将代码更改为以下内容。您的代码中存在许多问题。您必须正确订购{},如果您这样做,则需要初始化foundIt

    Scanner input = new Scanner(System.in);
    //lets create the array
    int[][] arrayOfInts = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    //create search variables
    System.out.println("Enter the key number to search for in the array: ");
    int key = input.nextInt();
    boolean foundIt = false;
    for (int i = 0; i < arrayOfInts.length; i++) {
       for (int j = 0; j < arrayOfInts[i].length; j++) {
          if (arrayOfInts[i][j] == key) {
            System.out.println("found " + key + " at row " + i + " column " + j);
            // if found it will change the foundIt to true
            foundIt = true;
            }
        }
    }
    if (!foundIt) {
        System.out.println(key + "is not in the array");
    }

答案 3 :(得分:0)

// perform search using a for loop
    for (int i = 0; i < arrayOfInts.length; i++) {
        for (int j = 0; j < arrayOfInts[i].length; j++) {
            if (arrayOfInts[i][j] == key) {
                    System.out.println("found " + key + " at row " + i + " column "
                            + j);
                    return;         
            }
        }
    }
    System.out.println(key + "is not in the array");

我刚刚添加了一个返回,找到了所需的元素,并将else分支剪切/粘贴到循环的末尾。

答案 4 :(得分:0)

您的System.out仅在此if-block

if (arrayOfInts[i][j] == key)

所以如果你找不到什么东西就没有打印输出

我会这样做:

...
for (int i = 0; i <arrayOfInts.length; i++){
    for (int j = 0; j <arrayOfInts[i].length; j++){
        if (arrayOfInts[i][j] == key) {
            foundIt = true;
            // Tell where you found it
            System.out.println("found " + key + " at row " +i+ " column " +j);
        }
    }
}
// After all check whether you found something anytime
if(!foundIt){
    System.out.println(key + "is not in the array");  
}
...

答案 5 :(得分:0)

这是因为你的print语句if(foundIt)... else块在if(arrayOfInts [i] [j] == key)块内。这意味着如果未找到int,则在检查打印位置时,代码永远不会进入内部。 你可以移动&#34; not found&#34;到最后。 例如:

    boolean foundIt = false;
    // perform search using a for loop
    for (int i = 0; i < arrayOfInts.length; i++)
    {
        for (int j = 0; j < arrayOfInts[i].length; j++)
        {
            if (arrayOfInts[i][j] == key)
            {
                foundIt = true;
                System.out.println("found " + key + " at row " + i + " column " + j);
            }

        }
    }
    if (!foundIt)
    {
        System.out.println(key + "is not in the array");
    }

不要忘记先将foundIt初始化为false。

答案 6 :(得分:0)

我建议的解决方案是:

bool foundit=false;

for (int i = 0; i <arrayOfInts.length; i++){
        for (int j = 0; j <arrayOfInts[i].length; j++){
            if (arrayOfInts[i][j] == key)
            {
            foundIt = true;
            break;
            System.out.println("found " + key + " at row " +i+ " column " +j);
           }
      }
}

if(!foundit)
{
system.out.println("Key not found in the array.")
}

答案 7 :(得分:0)

这有效:

import java.util.Scanner;
public class ArraySearch {
public static void main (String[] args){
    Scanner input = new Scanner(System.in);

    //lets create the array
    int [] [] arrayOfInts = {{1, 2,3,4}, {5,6,7,8},{9,10,11,12}};

    //create search variables
    System.out.println("Enter the key number to search for in the array: ");
    int key = input.nextInt();

    //perform search using a for loop
    for (int i = 0; i <arrayOfInts.length; i++){
        for (int j = 0; j <arrayOfInts[i].length; j++){
            if (arrayOfInts[i][j] == key) {
                System.out.println("found " + key + " at row " +i+ " column " +j);
                return;
            }
        }
    }
    System.out.println(key + " is not in the array");
}
}

为什么: 只有在数组中找到元素时,operator if (arrayOfInts[i][j] == key) {}中的命令才会执行,因此不需要使用boolean foundIt;。使用return结束执行课程,因为我们找到了我们想要的东西。行System.out.println(key + " is not in the array");应该在两个周期之后,因此只有在我们之前检查过二维数组的每个元素时它才会起作用。