Java - Write code to get the index of an element in an array

Requirements

Write code to get the index of an element in an array

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String el = "book2s.com";
        String[] ar = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(index(el, ar));
    }/*w w  w .j a v a 2s  . co  m*/

    /**
     * @return the index of the first element in "ar" that is equals to "el" or
     *         -1 (if there is no element equal to el).
     */
    public static int index(final String el, final String[] ar) {
        for (int i = 0; i < ar.length; ++i) {
            if (ar[i].equals(el))
                return i;
        }
        return -1;
    }
}