[英]Generating random numbers and sorting them out in Java
My goal is to generate random number from 0 to 100 and add them into a linkedlist object and then sort the elements. 我的目标是生成0到100之间的随机数,并将它们添加到链表列表对象中,然后对元素进行排序。
This is my code so far. 到目前为止这是我的代码。 I'm running into problems when I want to display the sorted elements.
当我想显示已排序的元素时,我遇到了问题。
The error I get: Exception in thread "main" java.util.IllegalFormatConversionException: d != java.util.Arrays$ArrayList 我得到的错误:线程“main”中的异常java.util.IllegalFormatConversionException:d!= java.util.Arrays $ ArrayList
Can someone throw some light into 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.
}
You need a List<Integer>
and not a List<int[]>
. 您需要
List<Integer>
而不是List<int[]>
。 Converting from a primitive array into a list of Integers is not a one-call operation, you'll actuall have to iterate over the primitive array and add to the list one by one. 从原始数组转换为整数列表不是一次调用操作,您必须迭代原始数组并逐个添加到列表中。 I don't recommend this, especially since there is no reason to use the array in the first place.
我不建议这样做,特别是因为没有理由首先使用该阵列。 For reference, you need this:
作为参考,你需要这个:
final List<Integer> randomList = new LinkedList<>();
for (int i : random) randomList.add(i);
When you change that, this will work: 当你改变它,这将工作:
System.out.printf("Sorted Elements: %s ", values);
However, it would be much simpler to sort the array itself using Arrays.sort(myArray)
and then print using 但是,使用
Arrays.sort(myArray)
对数组本身进行Arrays.sort(myArray)
然后使用打印会更简单
System.out.println(Arrays.toString(myArray));
On the other hand, if you used a List<Integer>
right from the start, it would look like this: 另一方面,如果您从一开始就使用
List<Integer>
,它将如下所示:
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);
Its getting confused because ** int[]
is an object but int
is not so it assumes int[]
as one element of the List
, and hence returning List<int[]>
not List<Integer>
or List<int>(this is not possible and is causing the issue)
. 它变得困惑,因为**
int[]
是一个对象但int
不是这样它假定int[]
作为List
一个元素,因此返回List<int[]>
而不是List<Integer>
或List<int>(this is not possible and is causing the issue)
。
Please change your random to Integer[]
as below, it should work fine. 请将随机更改为
Integer[]
,如下所示,它应该可以正常工作。
Integer [] random = new Integer[100];
List<Integer> randomList = Arrays.asList(random);
To sort the list: Use Collections#sort
要对列表进行排序:使用
Collections#sort
Collections.sort(randomList);
Please 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 argument type being passed. 请记住 ,方法签名是:
public static <T> List<T> asList(T... a)
,它根据传递的参数类型确定要返回的List的类型。
Please Note: Even when you define random
as new Integer[100];
请注意:即使你将
random
定义为new Integer[100];
, still you can leave this kind statements as is: random[i] = r.nextInt(100) + 1;
,你仍然可以保留这种语句:
random[i] = r.nextInt(100) + 1;
. 。 This works fine as
int
is promoted to Integer
in those cases. 这样可以正常工作,因为在这些情况下
int
被提升为Integer
。
In this case, I think it would be easier to pass the random
array directly to a sortElements(int[] values)
method and then convert the array to a list. 在这种情况下,我认为将
random
数组直接传递给sortElements(int[] values)
方法然后将数组转换为列表会更容易。
In other words, you would call sortElements(random);
换句话说,你会调用
sortElements(random);
and then List<Integer> randomList = new LinkedList(Arrays.asList(random));
然后
List<Integer> randomList = new LinkedList(Arrays.asList(random));
to get a linked list. 获取链表。
Using your code try: 使用您的代码尝试:
for (int[] is : set) {
System.out.printf("Sorted Elements: %d ", is[0]);
}
but you still need to sort it, as mentioned in previous post 但你还需要对它进行排序,如前一篇文章所述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.