簡體   English   中英

用整數值填充數組

[英]Filling array with integer values

我有以下代碼:

public static <T extends Comparable<T>> T[] getRandomPermutationOfIntegers(int size) {
      T[] data = (T[])new Comparable[size]; 
      for (int i = 0; i < size; i++) {
          data[i] = i;
      }
      // shuffle the array
      for (int i = 0; i < size; i++) {
          int temp;
          int swap = i + (int) ((size - i) * Math.random());
          temp = data[i];
          data[i] = data[swap];
          data[swap] = temp;
      }
      return data;
  }

排列整數數組並返回它們。 我想用int值填充數組,但是由於T與int不同,所以在兩個for()循環中出現錯誤。

我該如何修復它們以使其正常工作?

使用Integer包裝器而不是原始int。

暫無
暫無

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

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