Java Array Reverse reverseArray(final T[] arr)

Here you can find the source of reverseArray(final T[] arr)

Description

Reverses the given array for iteration.

License

Open Source License

Parameter

Parameter Description
T The content type.
arr The array to reverse.

Return

The reversed array.

Declaration

public static <T> Iterable<T> reverseArray(final T[] arr) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;

public class Main {
    /**//from   w w w. j av a2 s .c om
     * Reverses the given array for iteration. The actual array is not modified.
     *
     * @param <T> The content type.
     * @param arr The array to reverse.
     * @return The reversed array.
     */
    public static <T> Iterable<T> reverseArray(final T[] arr) {
        return new Iterable<T>() {

            @Override
            public Iterator<T> iterator() {
                return new Iterator<T>() {

                    private int pos = arr.length;

                    @Override
                    public boolean hasNext() {
                        return pos > 0;
                    }

                    @Override
                    public T next() {
                        return arr[--pos];
                    }

                    @Override
                    // TODO #43 -- Java 8 simplification
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }

                };
            }

        };
    }
}

Related

  1. reverse(T[] array)
  2. reverse(T[] array)
  3. reverse(T[] array)
  4. reverse(T[] array)
  5. reverse(T[] originalArray)
  6. reverseArray(Object[] arr)
  7. reverseArray(T[] arr)
  8. reverseArrayList(ArrayList listIn)
  9. reverseList (ArrayList l)

    HOME | Copyright © www.java2s.com 2016