gpt4 book ai didi

c# - 在多个数组列表中,我想重命名重复值

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:52 24 4
gpt4 key购买 nike

我尝试了这段代码,因为如果我在多个 arraylist 中有重复的字符串,它会更新 1_Maths,2_Maths

例如在 multiplearraylist 我有

maths

english

maths

hindi

english

所以我想要 o/p:

1_Maths

1_english

2_Maths hindi

2_english

我的代码是生成数组列表

Dim col As Integer = 0
Dim dt As New DataTable

Dim ArraySubject(0, Grd.Columns.Count - 1) As String
For i As Integer = 0 To Grd.Columns.Count - 1
If Grd.Columns(i).Visible = True Then
ArraySubject(0, col) = GvSearch.HeaderRow.Cells(i).Text
col += 1
End If
Next

用 C# 回答也会对我有帮助。

最佳答案

我不太了解 VB.NET,所以这就是我认为您想要的 C#:

string[] subjects = new string[] { "maths", "english", "maths", "hindi", "english" };

Dictionary<string, int> subjectCounts = new Dictionary<string, int> ();
foreach (string subject in subjects) {
if (subjectCounts.ContainsKey(subject))
subjectCounts[subject]++;
else
subjectCounts.Add(subject, 1);
}

List<string> output = new List<string>();
foreach (KeyValuePair<string, int> pair in subjectCounts) {
if (pair.Value > 1) {
for (int i = 1; i <= pair.Value; i++)
output.Add(i + "_" + pair.Key);
} else {
output.Add(pair.Key);
}
}

foreach (string subject in output)
Console.WriteLine(subject);

这个输出:

1_maths

2_maths

1_english

2_english

hindi

如果要按数字排序,在输出之前对数组进行排序即可。

output.Sort();

输出:

1_english

1_maths

2_english

2_maths

hindi

关于c# - 在多个数组列表中,我想重命名重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18029975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com