3

在 c# 中回答也对我有帮助。

我尝试了这段代码,如果我在多个数组列表中有重复的字符串,它会像以前一样按顺序更新和显示。

maths
english
maths
hindi
english
science
Economics
scince

我需要这样的输出

maths_1
english_1
maths_2
hindi
science_1
Economics
scince_2

我试过这段代码,但输出不按顺序**

Dim subjectCounts As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
            For Each subject As String In arraysub
                If subjectCounts.ContainsKey(subject) Then
                    subjectCounts(subject) = (subjectCounts(subject) + 1)
                Else
                    subjectCounts.Add(subject, 1)
                End If
            Next
            Dim output As List(Of String) = New List(Of String)

            For Each pair As KeyValuePair(Of String, Integer) In subjectCounts
                If (pair.Value > 1) Then
                    Dim i As Integer = 1
                    Do While (i <= pair.Value)
                        output.Add((i.ToString + ("_" + pair.Key)))

                        i = (i + 1)
                    Loop
                Else
                    output.Add(pair.Key)
                End If
            Next
4

3 回答 3

2

您的问题在于使用字典。字典没有排序,因此无论何时遍历它都不能保证顺序。但是 aList(Of KeyValuePair(Of String,Integer))会做你想做的工作。

此外,您可以使用相同的列表(字符串)来执行此操作。我很少使用arraylist,从来没有发现列表不能做的需要,但我想语法应该几乎相同。像这样的东西应该工作

Dim arraysub As List(Of String) = New List(Of String)({
"maths",
"english",
"maths",
"hindi",
"english",
"science",
"Economics",
"science"
})
For i = 0 To arraysub.Count - 1
    If Not Char.IsDigit(arraysub(i).Last) Then
        Dim temp As String = arraysub(i)
        For j = 0 To arraysub.FindAll(Function(s) s = arraysub(i)).Count - 1
            arraysub(arraysub.IndexOf(temp)) += "_" + (j + 1).ToString
        Next
    End If
Next

输出是:

?arraysub
Count = 8
    (0): "maths_1"
    (1): "english_1"
    (2): "maths_2"
    (3): "hindi_1"
    (4): "english_2"
    (5): "science_1"
    (6): "Economics_1"
    (7): "science_2"

这是使用数组列表的相同代码:

    Dim arraysub As ArrayList = New ArrayList(New String(7) {"maths", "english", "maths", "hindi", "english", "science", "Economics", "science"})
    For i = 0 To arraysub.Count - 1
        If Not Char.IsDigit(CStr(arraysub(i)).Last) Then
            Dim temp As String = CStr(arraysub(i))
            For j = 0 To Array.FindAll(arraysub.ToArray, Function(s) s Is CStr(arraysub(i))).Count - 1
                arraysub(arraysub.IndexOf(temp)) = CStr(CStr(arraysub(arraysub.IndexOf(temp))) & "_" + (j + 1).ToString)
            Next
        End If
    Next

并有这个输出:

?arraysub
Count = 8
    (0): "maths_1" {String}
    (1): "english_1" {String}
    (2): "maths_2" {String}
    (3): "hindi_1" {String}
    (4): "english_2" {String}
    (5): "science_1" {String}
    (6): "Economics_1" {String}
    (7): "science_2" {String}

你可以从中看出为什么很多人更喜欢 List 而不是 ArrayList。

于 2013-08-05T06:26:03.577 回答
2

我认为这会产生你想要的输出

首先让我们检查主题是否需要以“_#”结尾

现在我们遍历主题,并为每个出现一次以上的人添加 _# 结尾。顺序将与输入相同,因为我们运行它。计数将即时生成,因此这是正确的。

    Dim hasMultiple As New Dictionary(Of String, Boolean)
    For Each subject As String In arraysub
        If hasMultiple.ContainsKey(subject) Then
            hasMultiple(subject) =  True
        Else
            hasMultiple.Add(subject, False)
        End If
    Next

    Dim output As New List(Of String)
    Dim subCount As New Dictionary(Of String, Integer) 
    For Each subject As String In arraysub
        If Not subCount.ContainsKey(subject) Then
            subCount.Add(subject, 0)
        End If
        subCount(subject) += 1
        If hasMultiple(subject) Then
            output.Add(subject & "_" & subCount(subject))
        Else
            output.Add(subject)
        End If
    Next
于 2013-08-05T09:11:52.433 回答
1

检查查尔斯·布雷塔纳的这篇文章

像这样创建类,

     public class MultiDimDictList<K, T>: Dictionary<K, List<T>>  
       {
           public void Add(K key, T addObject)
           {
               if(!ContainsKey(key)) 
               {
               Add(key, new List<T>());
               base[key].Add(addObject);
               }else{
               for(int i=1; i<i+1;i++){
                if(!ContainsKey(key+"_"+i)){
                    Add(key+"_"+i+, new List<T>());
                    base[key+"_"+i].Add(addObject+"_"+i);       
                    break;
                  }
                }
              }  
           }           
       }

像下面这样称呼它,

myDicList.Add("YourKEY", "YourSUBJECT");

我只是根据您的要求进行了修改,但对此我不确定。

于 2013-08-05T06:57:11.853 回答