Java return index if value found in array

34,094

Solution 1

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 ==.

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.

Solution 2

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?).

You should use ints (rather then Integers) or change the condition to use the equals() method.

Solution 3

Integer is an object, so you have to make use of the equals method. Try numbers[index].equals(key).

Solution 4

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

or

org.apache.commons.lang.ArrayUtils.indexOf(accs, key);
Share:
34,094
JackyBoi
Author by

JackyBoi

Updated on July 09, 2022

Comments

  • JackyBoi
    JackyBoi almost 2 years

    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:

    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. Is something wrong there?

    • Vishy
      Vishy over 11 years
      Is there any reason you are using Integer rather than int ? What do you see in key and what were you expecting?
    • zeller
      zeller over 11 years
      You're using == for object comparison (which does reference comparison). You should use equals, or simple int. This code will only accidentally work when your numbers are in the Integer cache range
    • assylias
      assylias over 11 years
      Use equals instead of == to compare integers. Or use int instead.
    • dcernahoschi
      dcernahoschi over 11 years
      you are comparing Integer objects by reference. You need to use equals
    • mssb
      mssb over 11 years
      This code is working what is the wrong in here????
    • JackyBoi
      JackyBoi over 11 years
      @mssb really it is working? cause ok my static Integer[] accs = new Integer[20]; is populated from another method as it is a class variable, so when i ask user enter the number he wants to search then i debug and see what is the key value it is showing random number but not my number.. that is y i mentioned it is not working
    • JackyBoi
      JackyBoi over 11 years
      @PeterLawrey yes there is static Integer[] accs = new Integer[20]; is a class variable and so this is used some other place as well and i am using this there... accs.length, which i dont think is applicable for int correct?
    • Vishy
      Vishy over 11 years
      You can do int[] accs = new int[20]; int len = accs.length; If you use int youc an use == instead of having to use .equals() i.e. it would solve most of your problems and be more efficient (and shorter)
    • JackyBoi
      JackyBoi over 11 years
      @PeterLawrey let me give it a try