C#使用泛型實(shí)現(xiàn)刪除數(shù)組中重復(fù)元素
一、涉及到的知識(shí)點(diǎn)
1.List<T>泛型集合
List<T>泛型集合表示可通過(guò)索引訪問(wèn)的對(duì)象的強(qiáng)類型列表,提供了用于對(duì)列表進(jìn)行搜索、排序和操作的方法。List<T>泛型集合位于System.Collections.Generic命名空間下。
2.List<T>泛型集合的Add方法
List<T>泛型集合的Add方法用于將對(duì)象添加到List<T>的結(jié)尾處。語(yǔ)法格式如下:
public void Add(T item)
參數(shù)說(shuō)明
item:要添加到List<T>的末尾處的對(duì)象。對(duì)于引用類型,該值可以為null。
3.List<T>泛型集合的ToArray方法
List<T>泛型集合的ToArray方法用于將List<T>的元素復(fù)制到新數(shù)組中。語(yǔ)法格式如下:
public virtual void Add(Object key,Object value)
參數(shù)說(shuō)明返回值:一個(gè)數(shù)組,它包含List<T>的元素的副本。
4.string.Join()方法
這種方法簡(jiǎn)潔、適用。用于代替foreach遍歷輸出,沒(méi)治了。
int[] int_arrays = [38, 98, 368, 98, 698, 2998, 368, 5998];
textBox1!.Text = string.Join(", ", int_arrays);
5.Array.Sort(int[] array)方法
該方法對(duì)值類型數(shù)組進(jìn)行排序,并修改源數(shù)組。該方法并不生成新的數(shù)組,如果需要生成新數(shù)組,則定義一個(gè)新數(shù)組,拷貝數(shù)組到新數(shù)組,然后對(duì)新數(shù)組進(jìn)行排序。
int[] int_arrays = [38, 98, 368, 98, 698, 2998, 368, 5998];
textBox1!.Text = string.Join(", ", int_arrays);
Array.Sort(int_arrays); //對(duì)值數(shù)組排序
textBox2!.Text = string.Join(", ", int_arrays);
// 使用 Array.Sort() 方法對(duì)數(shù)組進(jìn)行排序
namespace _130_2
{
public class Program
{
public static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
int[] intArray = { 38, 98, 368, 98, 698, 2998, 368, 5998 };
Array.Sort(intArray);
Console.WriteLine("排序后的數(shù)組:");
foreach (int num in intArray)
{
Console.WriteLine(num);
}
}
}
}
//運(yùn)行結(jié)果
/*
排序后的數(shù)組:
38
98
98
368
368
698
2998
5998
*/6.HashSet<T> 泛型集合
使用泛型集合 HashSet<T> 來(lái)刪除數(shù)組中的重復(fù)數(shù)字。HashSet<T> 集合用于存儲(chǔ)唯一的元素,如果嘗試添加重復(fù)元素,它將自動(dòng)忽略重復(fù)項(xiàng)。
// 使用 HashSet<T> 去除數(shù)組中的重復(fù)數(shù)字
namespace _130_1
{
public class Program
{
public static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
int[] numbers = [1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9];
HashSet<int> uniqueNumbers = [.. numbers];
Console.WriteLine("去除重復(fù)數(shù)字后的數(shù)組:");
foreach (int number in uniqueNumbers)
{
Console.WriteLine(number);
}
}
}
}
//運(yùn)行結(jié)果:
/*
去除重復(fù)數(shù)字后的數(shù)組:
1
2
3
4
5
6
7
8
9
*/示例首先創(chuàng)建一個(gè)包含重復(fù)數(shù)字的數(shù)組。然后,創(chuàng)建一個(gè) HashSet<int> 集合來(lái)存儲(chǔ)唯一的數(shù)字。接下來(lái),使用 foreach 循環(huán)遍歷原始數(shù)組,并將每個(gè)數(shù)字添加到 HashSet<int> 集合中。由于 HashSet<int> 集合只存儲(chǔ)唯一的元素,重復(fù)的數(shù)字將被自動(dòng)忽略。最后,我們使用另一個(gè) foreach 循環(huán)打印去除重復(fù)數(shù)字后的結(jié)果。
二、實(shí)例
使用泛型去掉了數(shù)組中的重復(fù)數(shù)字,具體實(shí)現(xiàn)時(shí),首先需要對(duì)數(shù)組進(jìn)行排序,排序之后重復(fù)的數(shù)字肯定是相鄰的,這時(shí)只需要比較鄰近的數(shù)字是否相同即可。
1.源碼
// 使用泛型去掉數(shù)組中的重復(fù)數(shù)字
namespace _130
{
public partial class Form1 : Form
{
private Label? label1;
private Label? label2;
private Label? label3;
private static TextBox? textBox1;
private static TextBox? textBox2;
private static TextBox? textBox3;
public Form1()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(12, 16),
Name = "label1",
Size = new Size(56, 17),
TabIndex = 0,
Text = "源數(shù)組:"
};
//
// label2
//
label2 = new Label
{
AutoSize = true,
Location = new Point(12, 45),
Name = "label2",
Size = new Size(56, 17),
TabIndex = 1,
Text = "排序后:"
};
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(61, 10),
Name = "textBox1",
Size = new Size(240, 23),
TabIndex = 2
};
//
// textBox2
//
textBox2 = new TextBox
{
Location = new Point(61, 39),
Name = "textBox2",
Size = new Size(240, 23),
TabIndex = 3
};
//
// label3
//
label3 = new Label
{
AutoSize = true,
Location = new Point(12, 74),
Name = "label3",
Size = new Size(56, 17),
TabIndex = 4,
Text = "新數(shù)組:"
};
//
// textBox3
//
textBox3 = new TextBox
{
Location = new Point(61, 68),
Name = "textBox3",
Size = new Size(240, 23),
TabIndex = 5
};
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(314, 98);
Controls.Add(textBox2);
Controls.Add(textBox1);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(textBox3);
Controls.Add(label3);
Name = "Form1";
Text = "使用泛型去掉數(shù)組中的重復(fù)數(shù)字";
ResumeLayout(false);
PerformLayout();
MainMethod();
}
/// <summary>
/// 定義一維源數(shù)組
/// 并對(duì)其排序
/// </summary>
static void MainMethod()
{
int[] int_arrays = [38, 98, 368, 98, 698, 2998, 368, 5998];
textBox1!.Text = string.Join(", ", int_arrays);
Array.Sort(int_arrays); //對(duì)值數(shù)組排序
textBox2!.Text = string.Join(", ", int_arrays);
int[] newArrays = RemoveNum(int_arrays);//調(diào)用方法去掉重復(fù)數(shù)字
textBox3!.Text = string.Join(", ", newArrays);
}
#region 去掉數(shù)組中的重復(fù)數(shù)字
/// <summary>
/// 去掉數(shù)組中的重復(fù)數(shù)字
/// </summary>
/// <param name="int_Data">要去除重復(fù)數(shù)字的int數(shù)組</param>
/// <returns>取出重復(fù)數(shù)字之后的數(shù)組</returns>
static int[] RemoveNum(int[] int_Data)
{
List<int> list_Arrays = []; //實(shí)例化泛型集合
for (int i = 0; i < int_Data.Length - 1; i++) //循環(huán)訪問(wèn)源數(shù)組元素
{
if (int_Data[i] != int_Data[i + 1]) //判斷相鄰的值是否相同
{
list_Arrays.Add(int_Data[i]); //如果不同,將值添加到泛型集合中
}
}
list_Arrays.Add(int_Data[^1]); //將數(shù)組的最后一個(gè)元素添加到泛型集合中
return [.. list_Arrays]; //將泛型集合轉(zhuǎn)換為數(shù)組,并返回
}
#endregion
}
}本實(shí)例實(shí)現(xiàn)時(shí)主要用到了List<T>泛型集合及其Add方法、ToArray方法。
2.生成效果

到此這篇關(guān)于C#使用泛型實(shí)現(xiàn)刪除數(shù)組中重復(fù)元素的文章就介紹到這了,更多相關(guān)C#刪除數(shù)組重復(fù)元素內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#結(jié)合數(shù)據(jù)庫(kù)實(shí)現(xiàn)驗(yàn)證識(shí)別ID卡內(nèi)容的方法
這篇文章主要介紹了C#結(jié)合數(shù)據(jù)庫(kù)實(shí)現(xiàn)驗(yàn)證識(shí)別ID卡內(nèi)容的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-07-07
C#使用文件流FileStream和內(nèi)存流MemoryStream操作底層字節(jié)數(shù)組byte[]
這篇文章介紹了C#使用文件流FileStream和內(nèi)存流MemoryStream操作底層字節(jié)數(shù)組byte[]的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#實(shí)現(xiàn)抓取和分析網(wǎng)頁(yè)類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)抓取和分析網(wǎng)頁(yè)類,實(shí)例分析了C#抓取及分析網(wǎng)頁(yè)中文本及連接的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05
WinForm通過(guò)操作注冊(cè)表實(shí)現(xiàn)限制軟件使用次數(shù)的方法
這篇文章主要介紹了WinForm通過(guò)操作注冊(cè)表實(shí)現(xiàn)限制軟件使用次數(shù)的方法,結(jié)合實(shí)例形式分析了WinForm操作注冊(cè)表的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06

