15,843,498 members
See more:
I am doing an internship at a local software company and my trainer gave me the following task:

`Given an array of ints (any length), return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.`

What I have tried:

I found a relevant task which is "
Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}." The solution code for this task is as follows;

Java
```public int[] reverse3(int[] nums) {
int[] temp = {nums[2], nums[1], nums[0]};
return temp;
}```

Should I use
`if/else`
logic to reflect "an array of any length" (like I programmed below)? Or do we always need to set the number of length?

Java
```public int[] reverseArray(int[] nums) {
if (nums.length > 0){
int[] temp = {nums[anyNumber]...., nums[1], nums[0]};
return temp;
} else {
return error;
}```
Posted
Updated 11-May-21 4:44am

## Solution 2

That will only work if you know the number of elements at compile time, which is most unlikely. Given a bunch of pieces of paper (anything from 5 to 5000 say), how would you sort them into reverse order? The obvious answer is to start with the last page, and move them one at a time onto a new pile. So with an array, as long as you can find the length, then a simple loop will do it.

## Solution 3

Or ... you could just let the system do the work: Collections.reverse() in Java with Examples - GeeksforGeeks[^]

## Solution 1

When searching for
`java reverse array`
you will find Reverse Array Java Example | Examples Java Code Geeks - 2021[^]

[no name] 11-May-21 8:45am
I already checked that link, the problem is that that website only has the fixed number of elements (like String[] typesOfInsurance = {"Cat", "Dog", "Elephant"};). My trainer asked me to reverse an array with an infinite number of elements - I think it is simply impossible to make it happen.
Richard MacCutchan 11-May-21 9:01am
"I think it is simply impossible to make it happen"
If that were the case then many programs would stop working.
Dave Kreskowiak 11-May-21 11:23am
You're lying about that link. The various code samples do not assume a fixed number of elements in the arrays they are reversing. You're are making assumptions about the code without even trying to understand how the code works.

This is a really simple task you've been assigned. Working with arrays is a very basic things to do and you do it quite frequently. If you can't figure this one out, you've got some learning to do.

If you can't put the time into trying to understand the code samples, you're going to have a really difficult time learning to write code.
[no name] 11-May-21 16:06pm
TL;DR I am very stunned to encounter "well-mannered" Stackoverflow-style user even on this Platform.

Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
If a question is poorly phrased then either ask for clarification, IGNORE it, or edit the question and fix the problem. Insults are NOT welcome.
Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, NOT make them feel stupid.
Let's avoid commenting like "u must do something otherwise u will suck." It's not your problem if that someone sucks for any reasons. You are not their career advisor.
Dave Kreskowiak 11-May-21 19:08pm
The link provided shows you multiple ways of reversing an array, yet you say it doesn't.

Considering the experienced people are you tell you you're wrong about the content of that link and the noob (you) is saying it doesn't contain any information about what you're asking, I think I'd go with the word of the experienced people.

Nobody is going to write your code for you. Nobody can hit you with a magic wand to make you understand. YOU have to put the work in to experiment and read the documentation to try to understand, and do more experimentation.

Nobody can do that for you.

But you telling the experienced people that we don't know what we're talking about? That's rich.