[英]C# inserting numbers into a list box in sorted position
請原諒我的菜鳥第一篇文章。 我有一個大學作業的問題。 分配是一個“號碼列表管理器”,基本上用戶選擇一個單選按鈕(已排序或未排序),當他們單擊按鈕時,30 個號碼將被添加到尊重用戶選擇的列表框中。 我對未排序的代碼沒有任何問題。 講師特別希望我們將排序后的數字添加到列表中,並在添加時對它們進行排序,而不是生成整個列表然后對其進行排序。
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
}
這是我在未排序位置將數字添加到列表中的代碼。 我試過在網上查找,但我能看到的唯一方法是使用 for 循環將數字添加到列表中,然后使用另一個 for 循環對它們進行排序。
這是標准的片段:您必須使用自己的代碼在插入和刪除期間操作列表,並且不得使用自動執行任務的 C# 方法,例如插入未排序的列表只需要將新值放入下一個插入排序列表時當前最后一個條目之后的可用位置需要您的代碼定位插入點並移動任何更高值的條目以打開/釋放插入點以包含新值。
我不是要求任何人為我做這項工作,但即使是偽代碼也會非常感激
好的,首先我不確定這些檢查列表是否已滿是什么意思,難道你不能在每次按下按鈕時清除項目並遍歷 1 到 30 嗎?
無論如何,這是我為排序和未排序編寫的一些代碼:
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);
}
}
您已經在遍歷列表並讀取每個值,因此您需要做的就是檢查現有值是否大於您插入的值。 如果是這樣,您應該在您剛剛檢查的項目之前插入它。
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
}
請注意檢查新項目何時需要放在列表末尾。
我想這就是你的老師想要的,了解如何使用數組很重要,但在 C# 中對列表進行排序的最佳方法不是使用數組,而是使用 List 或其他具有自己排序方法的類。
var myList = new List<int>();
myList.Add(5);
myList.Add(3);
myList.Add(1);
myList.Sort();
如果您有一百萬個項目,這將更快、更有效,並且它適用於字符串或日期或其他任何內容。
List
還有一個Contains
方法,以及其他幾個讓生活更輕松的快捷方式。 @ThePerplexedOne 在填充列表后進行排序是正確的,您可以使用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.