简体   繁体   English

C#将数字插入到排序位置的列表框中

[英]C# inserting numbers into a list box in sorted position

Excuse my nooby first post please.请原谅我的菜鸟第一篇文章。 I am having an issue with a univerity assignment.我有一个大学作业的问题。 The assignment is a "Number list manager", basically user chooses a radio button (sorted or unsorted) and when they click a button 30 numbers will be added to a list box respecting the users choice.分配是一个“号码列表管理器”,基本上用户选择一个单选按钮(已排序或未排序),当他们单击按钮时,30 个号码将被添加到尊重用户选择的列表框中。 I have no issue with the unsorted code.我对未排序的代码没有任何问题。 The lecturer specifically wants us to add the sorted numbers into the list and sort them as they are being added rather than generate the whole list and then sorting it.讲师特别希望我们将排序后的数字添加到列表中,并在添加时对它们进行排序,而不是生成整个列表然后对其进行排序。

if (radUnsorted.Checked == true)//if unsorted
{
    RunChecks();
    do
    {
        numberToInput = (rnd.Next(0, 100));//sets number to input to random number
        int i = 1;

        while (i < lstNumbers.Items.Count)//loops though whole list
        {
            if (numberToInput == ConvertToInt32(lstNumbers.Items[i]))//checks that number doesnt already exist in list
            {
                numberToInput = (rnd.Next(0, 101));//genrate new random number
                i = 0;//set to 0 to restart checking through the list
            }
            else//if the number isnt at i index
            {
                i++;//move onto next number
            }
        }

        lstNumbers.Items.Insert(i - 1, numberToInput);//insert the number
        RunChecks();//check if the list is full

    } while (spaceLeft == true);//keep looping untill list is full
}

This is my code to add numbers to the list in unsorted position.这是我在未排序位置将数字添加到列表中的代码。 I've tried looking online but the only method i can see is a for loop to add the numbers to the list and then another for loop to sort them.我试过在网上查找,但我能看到的唯一方法是使用 for 循环将数字添加到列表中,然后使用另一个 for 循环对它们进行排序。

Here is a snippet of the criteria: You must manipulate the list during inserts and deletes using your own code and not must not utilise C# methods which undertake the task automatically eg inserting into an unsorted list merely requires the new value to be placed in the next available location after the current last entry whilst inserting into a sorted list requires your code to locate the insertion point and move any higher value entries to open/free up the insertion point for the inclusion of the new value.这是标准的片段:您必须使用自己的代码在插入和删除期间操作列表,并且不得使用自动执行任务的 C# 方法,例如插入未排序的列表只需要将新值放入下一个插入排序列表时当前最后一个条目之后的可用位置需要您的代码定位插入点并移动任何更高值的条目以打开/释放插入点以包含新值。

I'm not asking for anyone to do the work for me but even pseudo-code would be very appreciated我不是要求任何人为我做这项工作,但即使是伪代码也会非常感激

Okay, first of all I'm not sure what these checks for the list being full is all about, can't you just clear the items each time you press the button and iterate through 1 to 30?好的,首先我不确定这些检查列表是否已满是什么意思,难道你不能在每次按下按钮时清除项目并遍历 1 到 30 吗?

Anyway, here's some code I knocked up for both sorted and unsorted:无论如何,这是我为排序和未排序编写的一些代码:

private void unsorted_Click(object sender, EventArgs e)
{
    lstNumbers.Items.Clear(); //clear any existing numbers, and add a new 30.
    var rand = new Random();
    for (var i = 0; i < 30; i++)
    {
        var randNumber = rand.Next(0, 100);
        while (lstNumbers.Items.Contains(randNumber))
        {
            //generate new number until it's unique to the list.
            randNumber = rand.Next(0, 100);
        }
        lstNumbers.Items.Add(randNumber);
    }
}

private void sorted_Click(object sender, EventArgs e)
{
    lstNumbers.Items.Clear(); //clear any existing numbers, and add a new 30.
    var rand = new Random();
    for (var i = 0; i < 30; i++)
    {
        var randNumber = rand.Next(0, 100);
        while (lstNumbers.Items.Contains(randNumber))
        {
            //generate new number until it's unique to the list.
            randNumber = rand.Next(0, 100);
        }

        if (lstNumbers.Items.Count == 0)
        {
            //we have no items, obviously the default position would be 0.
            lstNumbers.Items.Add(randNumber);
            continue; //next iteration
        }

        //find out the sorted position
        var bestPos = 0;
        for (var j = 0; j < lstNumbers.Items.Count; j++) //loop through the current list.
        {
            var currValue = Convert.ToInt32(lstNumbers.Items[j]);
            if (randNumber > currValue)
            {
                bestPos = j + 1;
            }
            else
            {
                bestPos = j;
                break; //we no longer need to check, it will never be any less than this.
            }
        }
        if (bestPos < 0)
            bestPos = 0;
        lstNumbers.Items.Insert(bestPos, randNumber);
    }
}

You're already iterating through the list and reading each value, so all you need to do is check if the existing value is larger than the one you're inserting.您已经在遍历列表并读取每个值,因此您需要做的就是检查现有值是否大于您插入的值。 If so, you should insert it before the item you've just checked.如果是这样,您应该在您刚刚检查的项目之前插入它。

            var inserted = false;
            while (i < lstNumbers.Items.Count)//loops though whole list
            {
                if (numberToInput == Convert.ToInt32(lstNumbers.Items[i]))//checks that number doesnt already exist in list
                {
                    numberToInput = (rnd.Next(0, 101));//genrate new random number
                    i = 0;//set to 0 to restart checking through the list
                }
                else if (numberToInput < Convert.ToInt32(lstNumbers.Items[i])
                {
                    lstNumbers.Items.Insert(i - 1, numberToInput);//insert the number
                    inserted = true;
                } 
                else//if the number isnt at i index
                {
                    i++;//move onto next number
                }
            }

            if (!inserted)
            { 
                lstNumbers.Items.Insert(i - 1, numberToInput);//insert the number
            }

Note the check for when the new item needs to go at the end of the list.请注意检查新项目何时需要放在列表末尾。

I imagine this is what your teacher wants, and it's important to understand how to work with arrays, but really the best way to sort a list in C# is not to use an array but a List or other class that has it's own sorting method.我想这就是你的老师想要的,了解如何使用数组很重要,但在 C# 中对列表进行排序的最佳方法不是使用数组,而是使用 List 或其他具有自己排序方法的类。

        var myList = new List<int>();
        myList.Add(5);
        myList.Add(3);
        myList.Add(1);

        myList.Sort();

This would be faster and more efficient if you had a million items, and it would work for strings or dates or whatever.如果您有一百万个项目,这将更快、更有效,并且它适用于字符串或日期或其他任何内容。

List also has a Contains method, and several other shortcuts that make life easier. List还有一个Contains方法,以及其他几个让生活更轻松的快捷方式。 @ThePerplexedOne is right that this sorts after the list is populated, you could use a SortedList , which would automatically insert items in the right place. @ThePerplexedOne 在填充列表后进行排序是正确的,您可以使用SortedList ,它会自动在正确的位置插入项目。

I make perhaps more simple way to solve this problem using SortedList .我使用SortedList制作了可能更简单的方法来解决这个问题。

SortedList<int, object> sortedList = new SortedList<int, object>();

sortedList.Add(4, null);
sortedList.Add(1, null);
sortedList.Add(7, null);

foreach(KeyValuePair<int, object> k in sortedList)
{
    Console.WriteLine(k.Key);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM