Home >Java >javaTutorial >How to find specified elements in an array sequentially in java

How to find specified elements in an array sequentially in java

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2023-05-19 17:46:061045browse

Find the specified element in the array (sequential search)

   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:

How to find specified elements in an array sequentially in java

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete