Results 1 to 6 of 6
  1. #1
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default [SOLVED] A direct way to get an index from an array?

    Gang:
    Is there a direct way to get the index of an element from an array (in this case, all elements in the array are different)? Maybe something like the "indexOf" used in strings?. I've searched, but haven't found anything. It sorta lookin like I'm going to have to loop through the array comparing against the array elements.
    Thanks,
    CJSL

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could loop through the array trying to match your object with each item in the array.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default That's what I thought...

    That's what I thought. OK... thanks... I'll cook something up and post it.
    Thanks,
    CJSL

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Sample

    Since there doesn't appear to be direct way to obtain the index of an array object, here's a piece of code that can be used to get the object's index:

    Java Code:
    public int getArrayIndex(int[] myArray, obj myObject)
     {
       int ArraySize = Array.getLength(myArray);//get the size of the array
       for (int i = 0; i < ArraySize; i++)
          {
           if (intDaPosition[i] == intDaNewPositionNumber)
            {
             return(i);
            }
          }
      return(-1);//didn't find what I was looking for
     }
    Some comments:
    • The method takes two arguments: the array and the object (int, string, etc) that is to be searched for in the array
    • In this case the comparation in the "if" statement is for ints. If strings are to be searched then ".equals" or ".compareTo" should be used. Since I was searching for ints, I used "==" for comparing
    • The method returns the index of the object within the array if the object is found. If not found it returns "-1"

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    If your array is sorted, you can use java.util.Arrays#binarySearch.

    db

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Thanks, but it's not sorted

    Darryl:
    Unfortunately, the array is not sorted. Thanks for the tip anyway... will keep it in mind.
    Thanks & have a nice day,
    Chris

Similar Threads

  1. Replies: 0
    Last Post: 06-07-2008, 09:02 PM
  2. how direct her/ him to the Sun´s download page?
    By willemjav in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:03 PM
  3. Help with Flesch Index
    By L_22 in forum Advanced Java
    Replies: 1
    Last Post: 03-31-2008, 05:30 PM
  4. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 06:33 AM
  5. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •