Java array indexOf example | array indexof java
JavaarrayindexOfexampleshowshowtofindindexofelementinarray.TheexamplealsoshowshowtofindindexofelementinprimitiveaswellasobjectarraysandcustomJavaarrayindexOfmethodimplementation.Howtofindtheindexofanelementinanarray?Javaarrays[1]areobjects,however,theydonotprovideanindexOfmethod.Inordertosearchtheelementandgetitsindex,weneedtoimplementourownindexOfmethod.Hereisthesamplecodeforthat.12345678910111213141516171819202122232425262728packagecom.javacodeexamples.basic.arrayexamples; importjava.util....
Java array indexOf example shows how to find index of element in array. The example also shows how to find index of element in primitive as well as object arrays and custom Java array indexOf method implementation.
How to find the index of an element in an array?Java arrays[1] are objects, however, they do not provide an indexOf method. In order to search the element and get its index, we need to implement our own indexOf method. Here is the sample code for that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.javacodeexamples.basic.arrayexamples;
import java.util.Arrays;
public class ArrayIndexOfExample {
public static void main(String[] args) {
//String array containing months
S...