C#: Array.Sort method for double?
本文关键字:for double method Sort Array | 更新日期: 2023-09-27 18:31:51
我有一个数组(双精度):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 + " ");
}