Java tutorial
//package com.java2s; /* * Copyright 1999-2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; public class Main { /** * Given an Object, and an index, it will get the nth value in the object. * <ul> * <li>If obj is a Map, get the nth value from the <b>key</b> iterator. * <li>If obj is a List or an array, get the nth value. * <li>If obj is an iterator, enumeration or Collection, get the nth value * from the iterator. * <li>Return the original obj. * </ul> * * @param obj * the object to get an index of * @param index * the index to get * @throws IndexOutOfBoundsException * @throws NoSuchElementException */ public static Object index(Object obj, int idx) { return index(obj, new Integer(idx)); } /** * Given an Object, and a key (index), it will get value associated with * that key in the Object. The following checks are made: * <ul> * <li>If obj is a Map, use the index as a key to get a value. If no match * continue. * <li>Check key is an Integer. If not, return the object passed in. * <li>If obj is a Map, get the nth value from the <b>key</b> iterator. * <li>If obj is a List or an array, get the nth value. * <li>If obj is an iterator, enumeration or Collection, get the nth value * from the iterator. * <li>Return the original obj. * </ul> * * @param obj * the object to get an index of * @param index * the index to get * @return the object at the specified index * @throws IndexOutOfBoundsException * @throws NoSuchElementException */ public static Object index(Object obj, Object index) { if (obj instanceof Map) { Map map = (Map) obj; if (map.containsKey(index)) { return map.get(index); } } int idx = -1; if (index instanceof Integer) { idx = ((Integer) index).intValue(); } if (idx < 0) { return obj; } else if (obj instanceof Map) { Map map = (Map) obj; Iterator iterator = map.keySet().iterator(); return index(iterator, idx); } else if (obj instanceof List) { return ((List) obj).get(idx); } else if (obj instanceof Object[]) { return ((Object[]) obj)[idx]; } else if (obj instanceof Enumeration) { Enumeration enumeration = (Enumeration) obj; while (enumeration.hasMoreElements()) { idx--; if (idx == -1) { return enumeration.nextElement(); } else { enumeration.nextElement(); } } } else if (obj instanceof Iterator) { return index((Iterator) obj, idx); } else if (obj instanceof Collection) { Iterator iterator = ((Collection) obj).iterator(); return index(iterator, idx); } return obj; } private static Object index(Iterator iterator, int idx) { while (iterator.hasNext()) { idx--; if (idx == -1) { return iterator.next(); } else { iterator.next(); } } return iterator; } }