get element from Iterable By Index - Java java.util

Java examples for java.util:Iterable Element

Description

get element from Iterable By Index

Demo Code

// This program is free software: you can redistribute it and/or modify
//package com.java2s;

import java.util.Iterator;

public class Main {
    public static <T> T getByIndex(Iterable<T> iterable, int index) {
        T el = null;// ww w  . j av a2  s.  c  om
        Iterator<T> it = iterable.iterator();
        for (int i = 0; it.hasNext(); i++) {
            T cur = it.next();
            if (i == index) {
                el = cur;
                break;
            }
        }
        return el;
    }
}

Related Tutorials