简体   繁体   English

搜索数组并返回索引(如果找到特定值)

[英]Search an array and return index if a specific value is found

This is more of just a syntax error, as most of my code as it is runs perfectly fine. 这更多的只是语法错误,因为我的大多数代码都运行良好。 The issue is that if a value is not found within the array, I'd like the program to print it out. 问题是,如果在数组中找不到值,我希望程序将其打印出来。 Can't seem to find a place to write that part of code without the program writing out "Not found" on every line. 如果没有在每一行中写出“ Not found”,程序似乎找不到写那部分代码的地方。 Below is my code and console output. 下面是我的代码和控制台输出。 Thanks all. 谢谢大家

Code: 码:

 public static void main(String[ ] args)
 {
  final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
  final int[ ] EMPTY = new int[0];
  final int MINIMUM = 0;
  final int MAXIMUM = 16;

  int target;

  System.out.println("Searching for numbers in an array.");
  for (target = MINIMUM; target <= MAXIMUM; target++)
  {
      System.out.print("\nIs " + target + " in the array? ");
      {
          for (int index = 0; index < DATA.length; index++)
          {
               if ( DATA[index] == target )
                   System.out.printf("Yes! %d was found at index [%d]", target, index);
          }
      }
  }

Console Output: 控制台输出:

Searching for numbers in an array...

Is 0 in the array? 
Is 1 in the array? 
Is 2 in the array? Yes! 2 was found at index [0]
Is 3 in the array? 
Is 4 in the array? Yes! 4 was found at index [1]
Is 5 in the array? 
Is 6 in the array? Yes! 6 was found at index [2]
Is 7 in the array? 
Is 8 in the array? Yes! 8 was found at index [3]
Is 9 in the array? 
Is 10 in the array? Yes! 10 was found at index [4]
Is 11 in the array? 
Is 12 in the array? Yes! 12 was found at index [5]
Is 13 in the array? 
Is 14 in the array? Yes! 14 was found at index [6]
Is 15 in the array? 
Is 16 in the array? 

You should use a variable inside the for loop to tell if something was found, also you should exit for once you found something to avoid unnecesary search. 您应该在for循环中使用一个变量来告诉您是否找到了某些内容,也应该在找到某些内容后退出,以避免不必要的搜索。

public static void main(String[ ] args)
 {
  final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
  final int[ ] EMPTY = new int[0];
  final int MINIMUM = 0;
  final int MAXIMUM = 16;
  boolean foundElement;
  int target;

  System.out.println("Searching for numbers in an array.");
  for (target = MINIMUM; target <= MAXIMUM; target++)
  {
      System.out.print("\nIs " + target + " in the array? ");
      foundElement = false;
      for (int index = 0; (index < DATA.length && !foundElement); index++)
       {
             foundElement = (DATA[index] == target);
             if ( foundElement )
               System.out.printf("Yes! %d was found at index [%d]", target, index);                       
        }
        if (!foundElement)
          System.out.printf(" Not found");    
  }
}

Also, you can read this to learn some other ways to achieve array item searching: 4 ways to search object in array 另外,您可以阅读本文以了解实现数组项搜索的其他方法: 在数组中搜索对象的4种方法

A quick fix would be the following: 快速修复方法如下:

  • Add a boolean variable 添加一个布尔变量

  • Set the boolean variable to true if the element is found 如果找到元素,则将布尔变量设置为true

  • Write Not Found if the element is not found (ie the boolean variable is false) 如果找不到元素(即布尔变量为false),则写入未找到

PS: Don't forget to make the boolean to false before each loop PS:不要忘记在每个循环之前将布尔值设置为false

Use this after System.out.print("\\nIs " + target + " in the array? "); System.out.print("\\nIs " + target + " in the array? "); :

boolean found = false;
for (int index = 0; index < DATA.length; index++) {
    if ( DATA[index] == target ) {
        System.out.printf("Yes! %d was found at index [%d]", target, index);
        found = true;
        break;
    }
}
if (!found) {
    System.out.printf("No! %d was not found", target);
}

There are many ways of doing that, all works well... 有很多方法可以完成所有工作...

Here a example of how you can do: 以下是如何执行的示例:

    final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
    final int[ ] EMPTY = new int[0];
    final int MINIMUM = 0;
    final int MAXIMUM = 16;
    int foundIndex;
    int target;

    System.out.println("Searching for numbers in an array.");
    for (target = MINIMUM; target <= MAXIMUM; target++)
    {
        foundIndex = -1;
        System.out.print("\nIs " + target + " in the array? ");
        {
            for (int index = 0; index < DATA.length; index++)
            {
                if ( DATA[index] == target ){
                    foundIndex = index;
                    break;
                }
            }
        }
        if(foundIndex > -1)
            System.out.printf("Yes! %d was found at index [%d]", target, foundIndex);
        else
            System.out.printf("No! %d was not found", target);
    }

Another way is using a boolean to set if was found: 另一种方法是使用布尔值设置是否被发现:

    final int[ ] DATA = { 2, 4, 6, 8, 10, 12, 14 };
    final int[ ] EMPTY = new int[0];
    final int MINIMUM = 0;
    final int MAXIMUM = 16;
    boolean found;
    int target;

    System.out.println("Searching for numbers in an array.");
    for (target = MINIMUM; target <= MAXIMUM; target++)
    {
        found = false;
        System.out.print("\nIs " + target + " in the array? ");
        {
            for (int index = 0; index < DATA.length; index++)
            {
                if ( DATA[index] == target ){
                    found = true;
                    System.out.printf("Yes! %d was found at index [%d]", target, index);
                    break;
                }
            }
        }
        if(!found)
            System.out.printf("No! %d was not found", target);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM