集册 Java实例教程 按索引在数组中搜索项

按索引在数组中搜索项

欢马劈雪     最近更新时间:2020-01-02 10:19:05

568
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
按索引在数组中搜索项


//package com.nowjava;/** N o w J a v a . c o m - 时代Java 提 供 **/


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));

    }


    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;
        /**
         from
        * nowjava - 时代Java 
        **/

    }


    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;

    }


    
展开阅读全文