C# 是否使用数组对列表框中的项目进行排序?

C# 是否使用数组对列表框中的项目进行排序?,c#,C#,好的,所以我必须使用数组(defaultArray)为大学对列表框(randomListBox)进行排序,但是说明和指导没有多大帮助,所以这是我使用说明得到的结果,但它不起作用 string[] defaultArray = new string[randomListBox.Items.Count]; for (int i = 0; i < randomListBox.Items.Count; i++) { defaultArray[i] = ra

好的,所以我必须使用数组(defaultArray)为大学对列表框(randomListBox)进行排序,但是说明和指导没有多大帮助,所以这是我使用说明得到的结果,但它不起作用

    string[] defaultArray = new string[randomListBox.Items.Count];

    for (int i = 0; i < randomListBox.Items.Count; i++)
    {
        defaultArray[i] = randomListBox.Items[i].ToString();
    }

    Array.Sort(defaultArray);

    randomListBox.Items.Clear();

    for (int i = 0; i < randomListBox.Items.Count; i++)
    {
        randomListBox.Items.Add(defaultArray[i].ToString());
    }
string[]defaultArray=新字符串[randomListBox.Items.Count];
对于(int i=0;i
想想这一行的第二次出现:

for (int i = 0; i < randomListBox.Items.Count; i++)
for(int i=0;i
你确定这就是你的意思吗

你到底想循环什么


(我不直接提供答案,因为这是一个家庭作业问题。)

您的第二个for循环从未运行,因为您已删除列表框中的所有项目,因此项目计数为
0
。尝试:

for (int i = 0; i < defaultArray.Length; i++)
{
    randomListBox.Items.Add(defaultArray[i]);
}
for(int i=0;i
在最后一个for循环中,您使用的是randomListBox.Items.Count,而不是defaultArray.Length

for (int i = 0; i < defaultArray.Length; i++)
{
    randomListBox.Items.Add(defaultArray[i].ToString());
}
for(int i=0;i
什么不起作用?最后一行,当它运行时,不会将任何内容放回ListBox,它不起作用不是问题。任何错误或问题?我想如果它不起作用,那就是一个问题。。。。。