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); } }
2 Answers
+ 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));
0
Thank you Avinesh