簡體   English   中英

生成隨機數並在Java中對其進行排序

[英]Generating random numbers and sorting them out in Java

我的目標是生成0到100之間的隨機數,並將它們添加到鏈表列表對象中,然后對元素進行排序。

到目前為止這是我的代碼。 當我想顯示已排序的元素時,我遇到了問題。

我得到的錯誤:線程“main”中的異常java.util.IllegalFormatConversionException:d!= java.util.Arrays $ ArrayList

有人可以解決這個問題嗎? 謝謝

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.

}

您需要List<Integer>而不是List<int[]> 從原始數組轉換為整數列表不是一次調用操作,您必須迭代原始數組並逐個添加到列表中。 我不建議這樣做,特別是因為沒有理由首先使用該陣列。 作為參考,你需要這個:

final List<Integer> randomList = new LinkedList<>();
for (int i : random) randomList.add(i);

當你改變它,這將工作:

System.out.printf("Sorted Elements: %s ", values);

但是,使用Arrays.sort(myArray)對數組本身進行Arrays.sort(myArray)然后使用打印會更簡單

System.out.println(Arrays.toString(myArray));

另一方面,如果您從一開始就使用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);

它變得困惑,因為** int[]是一個對象但int不是這樣它假定int[]作為List一個元素,因此返回List<int[]>而不是List<Integer>List<int>(this is not possible and is causing the issue)

請將隨機更改為Integer[] ,如下所示,它應該可以正常工作。

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

要對列表進行排序:使用Collections#sort

    Collections.sort(randomList);

請記住 ,方法簽名是: public static <T> List<T> asList(T... a) ,它根據傳遞的參數類型確定要返回的List的類型。

請注意:即使你將random定義為new Integer[100]; ,你仍然可以保留這種語句: random[i] = r.nextInt(100) + 1; 這樣可以正常工作,因為在這些情況下int被提升為Integer

在這種情況下,我認為將random數組直接傳遞給sortElements(int[] values)方法然后將數組轉換為列表會更容易。

換句話說,你會調用sortElements(random); 然后List<Integer> randomList = new LinkedList(Arrays.asList(random)); 獲取鏈表。

使用您的代碼嘗試:

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

但你還需要對它進行排序,如前一篇文章所述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM