Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:58:22+00:00 2026-06-14T12:58:22+00:00

I am trying to reverse an array in 2 ways: 1) By creating a

  • 0

I am trying to reverse an array in 2 ways:

1) By creating a new array which was very easy:

public static int[] reverse(int[] array) {
    int[] reverseArray = new int[array.length];
    for(int i = 0; i < reverseArray.length; i++) {
        reverseArray[i] = array[array.length - i - 1];
    }
    return reverseArray;
}

2) The second method I got my answer but I actually don’t understand it very well, it actually makes use of swapping, giving the value of the array to a temporary variable then changes it and returns it to the original variable:

public static int[] reverse2(int[] array)
{
    for (int i=0; i < array.length / 2; i++)
    {   
        int temp = array[i];
        array[i] = array[array.length - i - 1];
        array[array.length - i - 1] = temp;
    }
    return array;
}

Could someone explain to me the second code?
I don’t understand the division by 2?
What happens if the array size is even or odd?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:58:23+00:00Added an answer on June 14, 2026 at 12:58 pm

    The division by 2 is merely so you only go through the first half of the array. If you swap the first and last items, you don’t want to do it again when i reaches array.length. If the size is even, it will stop before the second half, if the size is odd, it will stop before the center position, which doesn’t need to be switched anyway. Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to reverse an int array in Java. This method does not
I am trying to reverse the order of an Array in java. What is
I am learning C and I'm trying to reverse each string in an array
I am trying to create an array for years which i will use in
Im learning arrays and Im trying to reverse the order in an array. This
I am trying to sort an array and find which one is bigger. So
I'm trying to reverse words in an array of string variables, but split doesn't
I'm trying to reverse string elements inside array, but I'm getting exact string order.
I am currently trying to create an array and display it reverse or descending
I am trying to display an array which by itself is no issue. However,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.