简体   繁体   English

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

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

I have an array ( double ) : ox_test with a known number of elements. 我有一个数组(double):ox_test具有已知数量的元素。

when I code : 当我编码时:

Array.Sort(ox_test);

and then, just to see if the array it's sorted : 然后,仅查看数组是否已排序:

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

.. I get ... 0, 0, 0, 0, 0 ( if the number of elements was 5 ). ..我得到... 0、0、0、0、0(如果元素数为5)。 Please help, thank you! 请帮忙,谢谢!

So i modified the both for loops: 所以我修改了两个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

You should start from 0 instead of 1. 您应该从0开始而不是1。

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

Also, please be sure of initialization of ox_test array. 另外,请确保ox_test数组的初始化。

Try with this code: 尝试使用以下代码:

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