Computer Science, asked by Smarty9165, 10 months ago

How I can reverse a Java Array?

Answers

Answered by Sreearjunan
0

Answer:The third method is to use the function java.util.Collections.reverse(List list) method. This method reverses the elements in the specified list. Hence, we convert the array into a list first by using java.util.Arrays.asList(array) and then reverse the list.

Answered by Abhishekalbert
0

To reverse an array in java 1st we have to know the requirements, so let's take an example.

if the user enters the array elements as 1, 2, 3, 4, 5 then the program would reverse the array and the elements of the array would be 5, 4, 3, 2, 1. To understand this program, you should have knowledge of following Java Programming topics:

Arrays in Java

Java For loop

Java While loop

I did not program before in JAVA so here an algo from where you can learn easily

The first method is as follows:

(i) Take input the size of array and the elements of array.

(ii) Consider a function reverse which takes the parameters-the array(say arr) and the size of the array(say n).

(iii) Inside the function, a new array (with the array size of the first array, arr) is initialized. The array arr[] is iterated from the first element and each element of array arr[] is placed in the new array from the back, i.e, the new array is iterated from its last element.

(iv) In this way, all the elements of the array arr[] are placed reversely in the new array.

(v) Further, we can iterate through the new array from the beginning and print the elements of the array.

Similar questions