use*_*632 1 c# arrays sorting windows-8
static int[] scores = new int[100];
static int[] scorescopy;
public static int orderscores()
{
scorescopy = scores;
Array.Sort(scorescopy);
int sortingtoolb = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图获取我的初始数组的副本,然后尝试对该副本进行排序.但是,当我使用Array.Sort()函数时,我的第一个数组也会继续排序,但我想保留它.我试图在scorecopy上取消新的声明,但这并没有影响结果.
另外,有没有办法将数组中未使用的变量保持为null?(如果我没有使用它的所有部分,我会在数组的开头得到一堆0).
我在运行Windows 8.1 Pro的系统上使用Visual Studio Express 2012 for Windows 8.
分配时,数组仅复制对内存中相同数组的引用.您需要实际复制值才能工作:
public static int orderscores()
{
scorescopy = scores.ToArray(); // Using LINQ to "cheat" and make the copy simple
Array.Sort(scorescopy);
int sortingtoolb = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请注意,您可以在没有LINQ的情况下执行此操作:
scorescopy = new int[scores.Length];
Array.Copy(scores, scorescopy, scores.Length);
//... rest of your code
Run Code Online (Sandbox Code Playgroud)