Finding Array Element Index in Java?

  • Thread starter Thread starter stripes
  • Start date Start date
  • Tags Tags
    Array Index
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
stripes
Messages
262
Reaction score
0

Homework Statement



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:
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:
int number = stringName.indexOf(whatyou'researchingfor);

and then I can use 'number' later on as the index at which 'whatyou'researchingfor' showed up.

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
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: