提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
按索引在数组中搜索项
//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; }