Reverse an Array in Java

0 votes
I am new to Java, can someone help me with the method and code to reverse an Array? Thank you soo much
Mar 11, 2019 in Java by Suresh
799 views

2 answers to this question.

0 votes

To reverse an array, here is the method:

public class RevArray {
   public static void main(String[] args) {
      int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      System.out.println("Array before reverse:");
      
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      } 
      for (int i = 0; i < arr.length / 2; i++) {
         int temp = arr[i];
         arr[i] = arr[arr.length - 1 - i];
         arr[arr.length - 1 - i] = temp;
      } 
      System.out.println("\nArray after reverse:");
      for (int i = 0; i < arr.length; i++) {
         System.out.print(arr[i] + " ");
      } 
   }
}
answered Mar 11, 2019 by Esha Gupta
0 votes

If you want to reverse the array in-place:

Collections.reverse(Arrays.asList(array));

It works since Arrays.asList returns a write-through proxy to the original array.

answered Aug 28, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
1 answer

How to reverse an int array in Java?

for(int i = 0; i < Data.length ...READ MORE

answered May 11, 2018 in Java by Akrati
• 3,190 points
987 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by Parth
• 4,640 points
1,194 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,172 views
+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,190 points
1,127 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,319 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,178 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,570 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,220 points
984 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 17, 2018 in Java by samarth295
• 2,220 points
1,557 views
0 votes
1 answer

Merge two array in Java

This is very simple, you need an ...READ MORE

answered Mar 11, 2019 in Java by Esha Gupta
1,093 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP