簡體   English   中英

C#:Array.Sort方法的兩倍?

[英]C#: Array.Sort method for double?

我有一個數組(double):ox_test具有已知數量的元素。

當我編碼時:

Array.Sort(ox_test);

然后,僅查看數組是否已排序:

for (int y = 1; y <= ox_test.Length; y++)
     MessageBox.Show(".x: " + ox_test[y]);

..我得到... 0、0、0、0、0(如果元素數為5)。 請幫忙,謝謝!

所以我修改了兩個for循環:

for (int y = 0; y < ox_test.Length; y++)
    MessageBox.Show(".x: " + ox_test[y]);
    // HERE  i get the values not sorted but != 0

Array.Sort(ox_test);

for (int y = 0; y < ox_test.Length; y++)
    MessageBox.Show(".x s: " + ox_test[y]);
    // HERE i get only 0 values
// sort double array
double[] doubleArray = new double[5] { 8.1, 10.2, 2.5, 6.7, 3.3 };
Array.Sort(doubleArray);
// write array
foreach (double d in doubleArray) Console.Write(d + " ");  // output: 2.5 3.3 6.7 8.1 10.2

您應該從0開始而不是1。

for (int y = 0; y < ox_test.Length; y++)
    MessageBox.Show(".x: " + ox_test[y]);

另外,請確保ox_test數組的初始化。

嘗試使用以下代碼:

Array.Sort(ox_test);

foreach (double i in ox_test)
{
    Console.Write(i + " ");
}

暫無
暫無

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

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