Home >Java >javaTutorial >How to find specified elements in an array sequentially in java
public static int findNum(int[] array,int key){ for (int i = 0; i <array.length ; i++) { if(array[i] == key){ return i; } } return -1; } public static void main(String[] args) { int[] array = {2,4,5,6,11,7,8,9}; System.out.println(findNum(array, 7)); }
Print result:
The above is the detailed content of How to find specified elements in an array sequentially in java. For more information, please follow other related articles on the PHP Chinese website!