1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Finding the index in an array

  1. May 28, 2012 #1
    1. The problem statement, all variables and given/known data

    If I am given a regular, single dimensional array in Java, how do I search for an element in it, and then how do I get the index of that element?

    I have been trying:

    Code (Text):

    int largest;  //some number that has been predetermined in earlier code
    int index3 = data.indexOf(largest);
    But it keeps giving me errors in compiling. This method doesn't exist or something. I've read online and indexOf is very confusing. Not sure why I can't simply look up the format of it. I always figured it was

    Code (Text):
    int number = stringName.indexOf(whatyou'researchingfor);
    and then I can use 'number' later on as the index at which 'whatyou'researchingfor' showed up.

    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. May 28, 2012 #2

    Borg

    User Avatar
    Science Advisor
    Gold Member

    Is the data variable your array? If so, you can't directly evaluate it that way (array doesn't have an indexOf method). You need to create a loop and examine the String values in the array one at a time.
     
    Last edited: May 28, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook