Reverses an array (Same implementation as string reversal - Java Collection Framework

Java examples for Collection Framework:Array Join

Description

Reverses an array (Same implementation as string reversal

Demo Code


//package com.java2s;

public class Main {
    /**/*from  w  ww .  j  a va  2  s.c om*/
     * Reverses an array (Same implementation as string reversal
     * @param list
     */
    public static void reverseArray(Character[] list) {
        reverseArrayFromToPosition(list, 0, list.length - 1);
    }

    public static void reverseArrayFromToPosition(Character[] list,
            int fromPos, int toPos) {
        //System.out.println("from Pos: " + fromPos + " to Pos" + toPos);
        int numIterations = (int) Math.ceil((toPos - fromPos) / 2.0);
        for (int x = fromPos; x < fromPos + numIterations; x++) {
            char temp = list[x];
            list[x] = list[toPos + fromPos - x];
            list[toPos + fromPos - x] = temp;
        }
    }
}

Related Tutorials