How to Reverse Array In Java

Connect with

How to Reverse Array In JavaLearn how to Reverse Array In Java using in-place swipe by character for loop, usng in-place swipe by for loop. Java example code for reversing of String or string array or int array.

1. Overview to Reverse Array in Java

Well, this is my second post to reverse a string. First one is reverse string in Java . In this post, I explained, how to reverse either string array or int array in Java. Although there are different ways to do this, I choose in place reversing. You can reverse a string by using the library ( prefer library in real-life day-to-day activity in the real project). There are different ways: iteration, recursive, in place. This is one of the important interview questions which is being asked frequently especially in the initial career.

2. In-place swipe by For Loop

/** 
 *  Java Method to reverse array in Java with 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;
	}
}

3. In-place swipe by while Loop

In this section, you learn about how to reverse array in Java using in-place swipe by while loop.

/**
 * This Java method to reverse a String in-place using a 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--;
	}
}

4. Complete source Code Example

In this section you find complete source code for reversing of array in Java, first convert string to array and then reverser using in-place swiping method.

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 a 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;
		}
	}
	
}


5. 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]

4. Reference

I hope you enjoyed this post of behavioral design patterns, and you can visit data structure tutorial for more details.

Visit Wiki page.

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


Connect with

Leave a Comment

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