Random number generation and sorting in Java

My goal is to generate a random number from 0 to 100 and add them to the linked list object, and then sort the items.

This is my code. I run into problems when I want to display sorted items.

The error I get is: Exception in thread "main" java.util.IllegalFormatConversionException: d! = Java.util.Arrays $ ArrayList

Can someone shed light on this problem? Thank you.

package com.LinkedLists; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.LinkedList; import java.util.Random; import java.util.Set; public class InsertRandomElements { public static void main(String[] args) { // Create a random number object from 0 to 100. // Create an array object. Random r = new Random(); int[] random = new int[100]; // Insert random numbers into the array for (int i = 0; i < random.length; i++) { random[i] = r.nextInt(100) + 1; } // Printing out the unsorted array for (int i = 0; i < random.length; i++) { System.out.println(random[i]); } List<int[]> randomList = Arrays.asList(random); // Call the method in here. sortElements(randomList); } // Sort the elements private static void sortElements(Collection<int[]> values) { Set<int[]> set = new HashSet<int[]>(values); for (int[] is : set) { System.out.printf("Sorted Elements: %d ", values); } System.out.println(); } // Calculate Sum of the elements // Calculate floating point average of the elements. } 

+4
source share
4 answers

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); 
+2
source

It is confused because ** int[] is an object, but int does not assume int[] as a single List element and therefore returns List<int[]> not List<Integer> or List<int>(this is not possible and is causing the issue) .

Change your random to Integer[] as below, it should work fine.

  Integer [] random = new Integer[100]; List<Integer> randomList = Arrays.asList(random); 

To sort the list: use Collections#sort

  Collections.sort(randomList); 

Remember , the method signature is: public static <T> List<T> asList(T... a) , which determines the type of list to be returned, based on the type of argument passed.

Note: Even if you define random as new Integer[100]; , you can leave this expression of the form as follows: random[i] = r.nextInt(100) + 1; . This works great, as int in these cases progresses to Integer .

+2
source

In this case, I think it would be easier to pass the random array directly to the sortElements(int[] values) method, and then convert the array to a list.

In other words, you should call sortElements(random); and then List<Integer> randomList = new LinkedList(Arrays.asList(random)); to get a linked list.

0
source

Using the code try:

 for (int[] is : set) { System.out.printf("Sorted Elements: %d ", is[0]); } 

but you still need to sort it as indicated in the previous post

-1
source

Source: https://habr.com/ru/post/1444807/


All Articles