Finding Array Element Index in Java?

In summary, the conversation discusses how to search for an element in a regular, single dimensional array in Java and how to get the index of that element. The person has been trying to use the indexOf method but is encountering errors and confusion. They are advised to create a loop and examine the string values in the array individually.
  • #1
stripes
266
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
  • #2
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:

1. How do I find the index of a specific element in an array?

The index of an element in an array can be found by using the indexOf() method in JavaScript. This method takes in the element as an argument and returns the index of the first occurrence of the element in the array.

2. Can I find the index of multiple elements in an array?

Yes, you can find the index of multiple elements in an array by using a for loop or the forEach() method. Within the loop, you can use the indexOf() method to check for the index of each element and store them in a separate array or print them out.

3. What happens if the element is not present in the array?

If the element is not present in the array, the indexOf() method will return -1. This indicates that the element is not found in the array.

4. Can I find the index of an element in a multidimensional array?

Yes, you can find the index of an element in a multidimensional array by using nested loops. The outer loop will iterate through the rows and the inner loop will iterate through the columns to check for the index of the element.

5. Is there a way to find the last index of an element in an array?

Yes, you can use the lastIndexOf() method to find the last index of an element in an array. This method works similarly to the indexOf() method, but it starts searching from the end of the array instead of the beginning.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
751
  • Engineering and Comp Sci Homework Help
Replies
4
Views
923
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top