Search item in array by indexof - Java Collection Framework

Java examples for Collection Framework:Array Index

Description

Search item in array by indexof

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int content = 2;
        int[] nums = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(indexof(content, nums));
    }//from w w  w  .  ja va2 s . c  om

    public static int indexof(int content, int[] nums) {
        int x = -1;
        for (int i = 0; i < nums.length; i++) {
            if (content == nums[i]) {
                x = i;
            }
        }
        return x;
    }

    public static int indexof(String content, String[] nums) {
        int x = -1;
        for (int i = 0; i < nums.length; i++) {
            if (content.equals(nums[i])) {
                x = i;
            }
        }
        return x;
    }

    public static int indexof(Object content, Object[] nums) {
        int x = -1;
        for (int i = 0; i < nums.length; i++) {
            if (content.equals(nums[i])) {
                x = i;
            }
        }
        return x;
    }

    public static int indexof(char content, char[] nums) {
        int x = -1;
        for (int i = 0; i < nums.length; i++) {
            if (content == nums[i]) {
                x = i;
            }
        }
        return x;
    }

    public static int indexof(boolean content, boolean[] nums) {
        int x = -1;
        for (int i = 0; i < nums.length; i++) {
            if (content == nums[i]) {
                x = i;
            }
        }
        return x;
    }
}

Related Tutorials