java 生成随机数并用Java将它们排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13290468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating random numbers and sorting them out in Java
提问by AppSensei
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.
}
回答by Marko Topolnik
You need a List<Integer>
and not a 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:
你需要一个List<Integer>
而不是一个List<int[]>
. 从原始数组转换为整数列表不是一次调用操作,您实际上必须遍历原始数组并逐个添加到列表中。我不推荐这样做,特别是因为没有理由首先使用数组。作为参考,你需要这个:
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)
然后使用打印会简单得多
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);
回答by Yogendra Singh
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[]>
notList<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];
, still you can leave this kind statements as is: random[i] = r.nextInt(100) + 1;
. This works fine as int
is promoted to Integer
in those cases.
请注意:即使您定义random
为new Integer[100];
,您仍然可以保留这种声明:random[i] = r.nextInt(100) + 1;
。如能正常工作int
被提升到Integer
在这些情况下。
回答by irrelephant
In this case, I think it would be easier to pass the random
array directly to a sortElements(int[] values)
method and thenconvert the array to a list.
在这种情况下,我认为将random
数组直接传递给sortElements(int[] values)
方法然后将数组转换为列表会更容易。
In other words, you would call sortElements(random);
and then List<Integer> randomList = new LinkedList(Arrays.asList(random));
to get a linked list.
换句话说,你会调用sortElements(random);
然后List<Integer> randomList = new LinkedList(Arrays.asList(random));
获取一个链表。
回答by Bruno
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
但您仍然需要对其进行排序,如上一篇文章所述