多个arraylist重复主题名依次显示

时间:2013-08-05 06:07:51

标签: c# asp.net .net vb.net visual-studio-2008

c#中的答案对我有帮助。

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

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

3 个答案:

答案 0 :(得分:2)

您的问题在于使用字典。字典不是订购的,因此无论何时迭代它都无法保证订单。但是List(Of KeyValuePair(Of String,Integer))将完成您想要的工作。

此外,您可以使用相同的列表(Of String)来完成此操作。我不太使用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"

以下是使用arraylist的相同代码:

    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 over ArrayList。

答案 1 :(得分: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

答案 2 :(得分:1)

this

检查Charles Bretana帖子

创建这样的类,

     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");

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