How to Reverse Array In Java

Connect with

Oracle Java
Well this my second post to reverse of string. In this post I tried to write how to reverse either string array or int array in java. Although, there are different way do this but I choose in place. you can reverse a string by using library ( prefer library in real life day to day activity in real project). there are different ways: iteration, recursive, in place. This is one of the important interview question which is being asked frequently specially in initial career.

In-place swipe by For Loop

/** 
 *  Java Method to reverse String array in place.
 *  
 *  @param array 
 */
public static void reverseString(String[] array) {
	// base case if no need to do any processing.
	if (array == null || array.length < 2) {
		return;
	}
	
	for (int i = 0; i < array.length / 2; i++) {
		String temp = array[i];
		array[i] = array[array.length - 1 - i];
		array[array.length - 1 - i] = temp;
	}
}

In-place swipe by while Loop

/**
 * This Java method to reverse a String in-place using while loop.
 * 
 * @param str
 * @return
 */
public static void reverseInPlaceUsingWhileloop(String[] strArray) {
	if (strArray == null || strArray.length < 2) {
		return ;
	}
	//char[] charArray = str.toCharArray();
	int i = 0;
	int j = strArray.length - 1;
	while (i < j) {
		String temp = strArray[i]; 
		strArray[i] = strArray[j]; 
		strArray[j] = temp;
		i++;
		j--;
	}
}

Complete source Code Example


package com.mysoftkey.dsa;

import java.util.Arrays;

/**
 * This class is used to demonstrate to reverse the value in array .
 * 
 * @author ranjeet.jha
 *
 */
public class ReverseStringArray {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// reverse String array in place by using for loop 
		String[] inputArray = {"ranjeet", "kumar", "jha"};
		System.out.println("original string array: " + Arrays.toString(inputArray));
		reverseString(inputArray);
		System.out.println("reversed string array: " + Arrays.toString(inputArray));
		
		// Reversed by using while loop
		String[] languages = {"java", "spring", "c++"};
		System.out.println("Before reversed languages: " + Arrays.toString(languages));
		reverseInPlaceUsingWhileloop(languages);
		System.out.println("after reversed languages: " + Arrays.toString(languages));
	}

	/** 
	 *  Java Method to reverse String array in place.
	 *  
	 *  @param array 
	 */
	public static void reverseString(String[] array) {
		// base case if no need to do any processing.
		if (array == null || array.length < 2) {
			return;
		}
		
		for (int i = 0; i < array.length / 2; i++) {
			String temp = array[i];
			array[i] = array[array.length - 1 - i];
			array[array.length - 1 - i] = temp;
		}
	}
			
	/**
	 * This Java method to reverse a String in-place using while loop.
	 * 
	 * @param str
	 * @return
	 */
	public static void reverseInPlaceUsingWhileloop(String[] strArray) {
		if (strArray == null || strArray.length < 2) {
			return ;
		}
		//char[] charArray = str.toCharArray();
		int i = 0;
		int j = strArray.length - 1;
		while (i < j) {
			String temp = strArray[i]; 
			strArray[i] = strArray[j]; 
			strArray[j] = temp;
			i++;
			j--;
		}
	}
        
        /** 
	 *  Java Method to reverse int array in place.
	 *  
	 *  @param array 
	 */
	public static void reverseInt(int[] array) {
		if (array == null || array.length < 2) {
			return;
		}
		for (int i = 0; i < array.length / 2; i++) {
			int temp = array[i];
			array[i] = array[array.length - 1 - i];
			array[array.length - 1 - i] = temp;
		}
	}
	
}


Output of Program

original string array: [ranjeet, kumar, jha]
reversed string array: [jha, kumar, ranjeet]
Before reversed languages: [java, spring, c++]
after reversed languages: [c++, spring, java]

Suggestions are welcome to improve this post. Please write your comment.


Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *