简体   繁体   English

Java返回索引(如果在数组中找到值)

[英]Java return index if value found in array

I am trying to find a value in an array and I know for sure that this value will exist only once, so i am trying to find the value and return the index of the array where it is stored, if not found return -1 This is what I am trying to do: 我正在尝试在数组中找到一个值,并且我确定该值将仅存在一次,所以我正在尝试查找该值并返回存储它的数组的索引,如果找不到,则返回-1 This我正在尝试做的是:

static Integer[] accs = new Integer[20];
 public static int search()
    {
        Integer[] numbers;
        numbers = accs;
        Integer key;
        Scanner sc = new Scanner(System.in);
         System.out.println("Enter the Account Number:");        
         key = sc.nextInt();

        for (Integer index = 0; index < numbers.length; index++)
      {
           if ( numbers[index] == key )
                 return index;  //We found it!!!
      }
     // If we get to the end of the loop, a value has not yet
     // been returned.  We did not find the key in this array.
     return -1;

    }

When I run this even though I know that the value exists in the array, nothing is being displayed. 即使知道该值存在于数组中,运行此命令时,也不会显示任何内容。 I debugged and then I found out that the Key variable is not having the value of what I typed. 我进行了调试,然后发现Key变量没有我键入的值。 Is something wrong there? 那里出问题了吗?

Your array is storing Integer, which is a java Object, to test for equality of Java Object you need to call Object.equals method instead of == . 您的数组存储Integer(它是一个Java对象),以测试Java对象是否相等,您需要调用Object.equals方法而不是==

In your case I suggest you use an array of int instead of Integer , it is lighter in memory, support == and you are not using any of the features of Integer so you don't need them here. 在您的情况下,我建议您使用int数组代替Integer ,它的内存更轻,支持==并且您没有使用Integer任何功能,因此在这里不需要它们。

You are checking for identity when checking with operator== (Are the two references point to the exact same object?) and not equality (do they equal each other?). 您在使用operator==进行检查时正在检查身份 (两个引用是否指向完全相同的对象?)而不是相等 (它们彼此相等吗?)。

You should use int s (rather then Integer s) or change the condition to use the equals() method. 您应该使用int (而不是Integer )或更改条件以使用equals()方法。

Integer is an object, so you have to make use of the equals method. 整数是一个对象,因此您必须使用equals方法。 Try numbers[index].equals(key). 尝试数字[index] .equals(key)。

java.util.Arrays.asList(accs).indexOf(key)  

要么

org.apache.commons.lang.ArrayUtils.indexOf(accs, key);

Make sure you understand the difference between object equality and object identity. 确保您了解对象相等性和对象身份之间的区别。 You are using "==" to check if your value is in the array - this checks for identity, not for equality. 您正在使用“ ==”来检查您的值是否在数组中-这将检查身份,而不是相等性。 Use numbers[index].equals(key) instead. 请改用numbers[index].equals(key)

You need set value for each element before search. 搜索之前,需要为每个元素设置值。 The statement below only declare new array with 20 element with value = null static Integer[] accs = new Integer[20]; 下面的语句仅声明值为20的新数组,其值= null static Integer [] accs = new Integer [20]; Replace by: static Integer[] accs = new Integer[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 替换为:static Integer [] accs = new Integer [] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; and retest. 并重新测试。

Regards, 问候,

initialize the array like this way static Integer[] accs = {3,5,15,6,4,32,9}; 以这种方式初始化数组static Integer[] accs = {3,5,15,6,4,32,9}; and call search() method in the main. 并在主目录中调用search()方法。 your function work properly. 您的功能正常工作。

使用以下命令捕获用户输入BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

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