You need a List<Integer> , not a List<int[]> . Converting from a primitive array to a list of integers is not a one-call operation; you will have to iterate over the primitive array and add it one by one to the list. I do not recommend this, especially since there is no reason to use an array in the first place. For reference, you need the following:
final List<Integer> randomList = new LinkedList<>(); for (int i : random) randomList.add(i);
When you change this, this will work:
System.out.printf("Sorted Elements: %s ", values);
However, it would be much easier to sort the array using Arrays.sort(myArray) and then print using
System.out.println(Arrays.toString(myArray));
On the other hand, if you used List<Integer> from the very beginning, it would look like this:
final Random rnd = new Random(); final List<Integer> values = new ArrayList<>(); for (int i = 0; i < 100; i++) values.add(rnd.nextInt()); Collections.sort(values); System.out.println("Sorted Elements: " + values);
source share