This Java tips is about, how to reverse array in Java, mostly primitive
types e.g. int, long, double and String arrays. Despite
of Java’s rich Collection API, use of array is quite common, but standard JDK
doesn’t has great utility classes for Java. It’s difficult to convert between
Java Collection e.g. List,
Set
to primitive arrays. Java’s array utility class java.util.Arrays, though
offers some of critical functionalities like comparing
arrays in Java and support
to print arrays, It lacks lot of common features, such as combining
two arrays and reverse primitive or object array. Thankfully, Apache commons lang, an open
source library from Apache software foundation offers one interesting class ArrayUtils, which can
be used in conjunction with java.util.Arrays class to play
with both primitive and object array in Java. This API offers convenient overloaded
method to reverse different kinds of array in Java e.g. int, double, float,
log or Object arrays. On a similar note, you can also write your own utility
method to reverse Array in Java, and
this is even a good
programming question. For production usage, I personally prefer tried and
tested library methods instead of reinventing wheel. Apache commons lang fits
the bill, as it offer other convenient API to complement JDK. In this Java
tutorial, we will reverse int and String array in
Java using ArrayUtils to show How to reverse primitive
and object array in Java.
Reverse int and String array in Java - Example

import java.util.Arrays; import org.apache.commons.lang.ArrayUtils; /** * * Java program to reverse array using Apache commons ArrayUtils class. * In this example we will reverse both int array and String array to * show how to reverse both primitive and object array in Java. * * @author */ public class ReverseArrayExample { public static void main(String args[]) { int[] iArray = new int[] {101,102,103,104,105}; String[] sArray = new String[] {"one", "two", "three", "four", "five"}; // reverse int array using Apache commons ArrayUtils.reverse() method System.out.println("Original int array : " + Arrays.toString(iArray)); ArrayUtils.reverse(iArray); System.out.println("reversed int array : " + Arrays.toString(iArray)); // reverse String array using ArrayUtis class System.out.println("Original String array : " + Arrays.toString(sArray)); ArrayUtils.reverse(sArray); System.out.println("reversed String array in Java : " + Arrays.toString(sArray)); } } Output: Original int array : [101, 102, 103, 104, 105] reversed int array : [105, 104, 103, 102, 101] Original String array : [one, two, three, four, five] reversed String array in Java : [five, four, three, two, one]
That's all on How to reverse Array in Java. In this Java program, we have seen examples to reverse String and int array
using Apache commons ArrayUtils class.By the way there are couple
of other ways to reverse array as well, e.g. by converting
Array to List and than using Collections.reverse() method or
by using brute force and reverse array using algorithm. It depends upon, which
method you like. If you are doing practice and learning Java, try to do this
exercise using loops.
Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Algorithms and Data Structures - Part 1 and 2
Data Structures in Java 9 by Heinz Kabutz
Related Java Programming tutorials from Javarevisited Blog
7 comments :
Hi Javin gr8 article but if we go by the way of java..then this thing can also be achieved..
You can reverse an array like this:
public void reverse(Object [] a){
for(int i = 0; i < a.length / 2; i++){
Object temp = a[i]; // swap using temporary storage
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
}
It's worthy to note that it doesn't matter if the array length is an odd number, as the median value will remain unchanged. I have to admit that I haven't tested this but it should work.
For now please check the following program..http://ideone.com/3qqRAm
disappointed. i expected that you would provide algorithm for this. but you ended up in using open source library method.
public void swap(int[] arr,int a,int b){
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
public int[] reverseArray(int[] arr){
int size=arr.length-1;
for(int i=0;i<size;i++){
swap(arr,i,size--);
}
return arr;
}
/* let s1 and s2 be the two string that we have to compare.
we'll insert every character of the first string into a hashmap.
the character will be the key, whereas, the frequency of the character will be the value */
Hashmap hm = new Hashmap();
count = 1;
for (i=0; i1)
{
val--;
hm.put(j,val);
}
else
{
hm.remove(j);
}
else
{
System.out.println("Not anagrams");
}
}
if (hm.isEmpty())
{
System.out.println("Anagrams");
}
else
{
System.out.println("Not anagrams");
}
Disappointing...... Was looking for an algorithm.
Agree, bad answer.
It's better to write something like this:
void reverse(int[] arr) {
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
Agree, bad answer.
It's better to write something like this:
void reverse(int[] arr) {
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
this doesn't work
Read more: http://javarevisited.blogspot.com/2013/03/how-to-reverse-array-in-java-int-String-array-example.html#ixzz4jMYu3XQ5
Post a Comment