0

Why doesn't the array sort?

import java.util.*; public class Sum{ public static void main (String[] args) { Scanner sc=new Scanner(System.in); int i; System.out.println("Enter the size of an array"); int n=sc.nextInt(); int[] a=new int[n]; for(i=0;i<n;i++) { a[i]=sc.nextInt(); } Arrays. sort(a); System.out.println(a); } }

16th Apr 2020, 9:54 AM
Ashutosh Mulky
Ashutosh Mulky - avatar
2 odpowiedzi
+ 1
Hi Ashutosh Mulky I do not see anything wrong with the code. The array is sorted but just to remind you that arrays are objects in Java. So you cannot print objects directly. You can either write another for loop to print the array elements one by one. OR replace the last line with- System.out.println(Arrays.toString(a));
16th Apr 2020, 10:13 AM
Avinesh
Avinesh - avatar
0
Thank you Avinesh
16th Apr 2020, 10:16 AM
Ashutosh Mulky
Ashutosh Mulky - avatar