What's wrong in my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

29th Apr 2022, 6:50 AM
Harsha S
Harsha S - avatar
6 Answers
+ 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] + " "); } } }
29th Apr 2022, 7:12 AM
Thineshan Panchalingam
Thineshan Panchalingam - avatar
+ 1
Thineshan Panchalingam thanks very much
29th Apr 2022, 7:15 AM
Harsha S
Harsha S - avatar
0
Raul Ramirez I know, but I want to reverse the array
29th Apr 2022, 7:00 AM
Harsha S
Harsha S - avatar
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 - -
29th Apr 2022, 7:08 AM
Raul Ramirez
Raul Ramirez - avatar
0
Cat Pro thank you for trying to think
29th Apr 2022, 7:33 AM
Harsha S
Harsha S - avatar
- 1
Just print back to front
29th Apr 2022, 6:59 AM
Raul Ramirez
Raul Ramirez - avatar