集册 Java实例教程 从迭代结束返回迭代器的n:th项。

从迭代结束返回迭代器的n:th项。

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

447
返回迭代结束时迭代器的n:th项。

/**

 * Copyright (c) 2002-2013 "Neo Technology,"

 * Network Engine for Objects in Lund AB [http://neotechnology.com]

 *

 * This file is part of Neo4j.

 *

 * Neo4j is free software: you can redistribute it and/or modify

 * it under the terms of the GNU General Public License as published by

 * the Free Software Foundation, either version 3 of the License, or

 * (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

//package com.nowjava;


import java.util.ArrayDeque;
/*
n o w  j a v a  . c o m
*/


import java.util.Deque;

import java.util.Iterator;


import java.util.NoSuchElementException;


public class Main {

    /**

     * Returns the iterator's n:th item from the end of the iteration.

     * If the iterator has got less than n-1 items in it

     * {@link NoSuchElementException} is thrown.

     * 

     * @param <T> the type of elements in {@code iterator}.

     * @param iterator the {@link Iterator} to get elements from.

     * @param n the n:th item from the end to get.

     * @return the iterator's n:th item from the end of the iteration.

     * @throws NoSuchElementException if the iterator contains less than n-1 items.

     *///nowjava - 时代Java

    public static <T> T fromEnd(Iterator<T> iterator, int n) {

        return assertNotNull(iterator, fromEndOrNull(iterator, n));

    }


    /**

     * Returns the iterator's n:th item from the end of the iteration.

     * If the iterator has got less than n-1 items in it

     * {@link NoSuchElementException} is thrown.

     * 

     * @param <T> the type of elements in {@code iterator}.

     * @param iterable the {@link Iterable} to get elements from.

     * @param n the n:th item from the end to get.

     * @return the iterator's n:th item from the end of the iteration.

     * @throws NoSuchElementException if the iterator contains less than n-1 items.

     */

    public static <T> T fromEnd(Iterable<T> iterable, int n) {

        return fromEnd(iterable.iterator(), n);

    }


    private static <T> T assertNotNull(Iterator<T> iterator, T result) {

        if (result == null) {

            throw new NoSuchElementException("No element found in "

                    + iterator);

        }

        return result;

    }


    /**

     * Returns the iterator's n:th item from the end of the iteration.

     * If the iterator has got less than n-1 items in it {@code null} is returned.

     * 

     * @param <T> the type of elements in {@code iterator}.

     * @param iterator the {@link Iterator} to get elements from.

     * @param n the n:th item from the end to get.

     * @return the iterator's n:th item from the end of the iteration,

     * or {@code null} if the iterator doesn't contain that many items.

     */

    public static <T> T fromEndOrNull(Iterator<T> iterator, int n) {

        Deque<T> trail = new ArrayDeque<T>(n);

        while (iterator.hasNext()) {

            if (trail.size() > n) {

                trail.removeLast();

            }

            trail.addFirst(iterator.next());

        }

        return trail.size() == n + 1 ? trail.getLast() : null;

    }


    
展开阅读全文