0
What's wrong in my code?
I am trying to reverse an array in java is there any better way to do this? my approach: https://code.sololearn.com/ccPCYHVfpn68/?ref=app
6 ответов
+ 1
Arrays class => List asList(Object[] arr)
You need to pass Integer array not int array
------------------------------------------------------
import java.util.Arrays;
import java.util.Collections;
public class Test
{
public static void main(String[] args) {
Integer arr[] = {1, 2, 3};
Collections.reverse(Arrays.asList(arr));
for(int i=0; i<arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
+ 1
Thineshan Panchalingam thanks very much
0
Raul Ramirez I know, but I want to reverse the array
0
You could try implementing a function yourself for example:
i = 0
j = arr.length-1;
while ( i < j )
swap arr[i] with arr[j]
i++
j - -
0
Cat Pro
thank you for trying to think
- 1
Just print back to front