-5
class ReverseArrayElements1
{
  public static void main ( String[] args )
  {
    int[] values = {10, 20, 30, 40}; 
    int temp;

     System.out.println( "Original Array: " + values[0] + "\n" + values[1] +
                         "\n" + values[2] + "\n" + values[3]   );

    // reverse the order of the numbers in the array



    System.out.println( "Reversed Array: " + values[0] + "\n" + values[1] + "\n"
                         + values[2] + "\n" + values[3] );
   }
}

Task I need to complete the program so that the numbers in the array appear in reversed order. This does not mean that I can just need to display the elements in reverse order; I will actually move the last element in the array into the the first element of the array, and so on. I can't use a loop or create a new array.

The output should be

Original Array: 10 20 30 40 
Reversed Array: 40 30 20 10 
6
  • 3
    Are you allowed to use recursion? Commented Dec 3, 2019 at 18:13
  • No we are not allowed to use recursion Commented Dec 3, 2019 at 18:26
  • 1
    Without using iteration or recursion - this seems to be an impossible task. And also, it seems to be crowd sourcing a homework problem with little effort shown, so I am voting to close. See: stackoverflow.com/questions/11322514/… Commented Dec 3, 2019 at 18:30
  • This is what the teacher told me. I am lost as you. I don't really even know where to begin. Commented Dec 3, 2019 at 18:31
  • 1
    Can you swap them manually like temp = arr[0]; arr[0]= arr[3]; arr[3]=tmp; to swap border values, and then temp = arr[1]; arr[1]=arr[2]; arr[2]=tmp; to swap middle values. Commented Dec 3, 2019 at 18:35

1 Answer 1

2

If you're using Java 8:

import java.util.stream.IntStream;

// stuff

int[] reversed = IntStream.range(0,values.length).map(i -> values[values.length-i-1]).toArray();
Sign up to request clarification or add additional context in comments.

3 Comments

You can't use other libraries. That defeats the purpose
I was attempting to answer the question I was given. Since you didn't specify restriction of external libraries in your question, I believe I did my best. In that case, it sounds like this is an assignment to practice recursion - and seeing as this question doesn't show your prior attempts I am voting for this question to be closed.
Our teacher said to redefine the arraylist.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.