如何在vb.net中创建字符串查找

时间:2017-03-17 17:15:07

标签: vb.net

我有一组数据,我可能最好在表格/矩阵中保存。它是来自游戏的匹配统计数据的集合,其中有两个超过100个字符选择的5个字符的团队可用。在一场比赛中不能挑选两次角色。我试图做的事情我总结了每个角色在整批比赛中杀死其他角色的次数。我想象的是一个包含表格标题和表格第一列所有字符的表格。每个单元格中的值是该行的字符杀死该列字符的频率。

我的问题是我有这样的数据;

Tom    1 Sarah  4 Juliet  3 Joe
Dick   3 Sarah  2 Juliet  - Joe
Harry  1 Sarah  2 Juliet  5 Joe

Sarah  3 Tom    - Dick    1 Harry
Juliet 1 Tom    2 Dick    3 Harry
Joe    - Tom    4 Dick    - Harry

第一行意味着汤姆杀了萨拉一次,朱丽叶杀了4次,乔三次杀了。

我的问题是有超过100个角色名称(汤姆,朱丽叶等)所以我如何存储汤姆杀死萨拉多少次而没有每个角色有100个案例的精选案例陈述?理想情况下,我只想给每个字符一个数字,然后有一个正方形数组并存储这样的数字;

dim kills(100,100) as integer
(1,23) +=1
(1,34) +=4
etc.

在上面(1,23)可能是汤姆杀死莎拉,因为汤姆的身份证可能是1而莎拉可能是23岁。

2 个答案:

答案 0 :(得分:1)

我不是Linq专家,但是这样的事情怎么样:

Sub Main()
    Dim Kills As New Dictionary(Of String, Dictionary(Of String, Integer))
    Dim Names As String = "Tom,Dick,Harry,John,Sarah,Jill"
    Dim NameArray = Names.Split(",")
    ' Create a dictionary of dictionaries to hold the kills
    For Each killer As String In NameArray
        Dim newKill As New Dictionary(Of String, Integer)
        For Each victim As String In NameArray
            newKill.Add(victim, 0)
        Next
        Kills.Add(killer, newKill)
    Next

    ' Tom Kills Sarah
    Kills("Tom")("Sarah") += 1
    Kills("Tom")("Sarah") += 1
    ' Tom kills Jill
    Kills("Tom")("Jill") += 1
    Kills("Dick")("Tom") += 1
    Kills("Dick")("Sarah") += 1
    ' Get some numbers out
    Dim SarahDies = Kills.Sum(Function(k) k.Value("Sarah"))
    Dim JillDies = Kills.Sum(Function(k) k.Value("Jill"))
    Dim DickDies = Kills.Sum(Function(k) k.Value("Dick"))
    Dim TomKillsSarah = Kills("Tom")("Sarah")
End Sub

答案 1 :(得分:0)

这是另一个需要考虑的解决方案。使用自定义类来表示杀手/受害者的元组,并将其用作Dictionary中的键。

Public Class KillInfo
    Implements IEquatable(Of KillInfo)

    Public Property Killer As String
    Public Property Victim As String

    Public Sub New(killer As String, victim As String)
        Me.Killer = killer
        Me.Victim = victim
    End Sub

    Public Overrides Function GetHashCode() As Integer
        Return Killer.GetHashCode() Xor Victim.GetHashCode()
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        If obj Is Nothing OrElse Me.GetType() IsNot obj.GetType() Then
            Return False
        End If
        Return Equals(DirectCast(obj, KillInfo))
    End Function

    Public Overloads Function Equals(other As KillInfo) As Boolean Implements IEquatable(Of KillInfo).Equals
        Return other.Killer.Equals(Killer) AndAlso other.Victim.Equals(Victim)
    End Function
End Class

然后像这样使用它

Dim Names As String = "Tom,Dick,Harry,John,Sarah,Jill"
Dim NameArray = Names.Split(",")

Dim KillSum As New Dictionary(Of KillInfo, Integer)

For Each killer As String In NameArray
    For Each victim As String In NameArray
        KillSum.Add(New KillInfo(killer, victim), 0)
    Next
Next

KillSum(New KillInfo("Tom", "Dick")) += 10
KillSum(New KillInfo("Tom", "Dick")) += 23
KillSum(New KillInfo("Harry", "Jill")) += 56
KillSum(New KillInfo("Jill", "Harry")) += 25

For Each info In KillSum
    Console.WriteLine($"{info.Key.Killer} killed {info.Key.Victim} {info.Value} times.")
Next

注意:我不知道Tuple(Of K,V)是否会以一种可用于词典的方式自行实现GetHashCode()Equals(),这就是为什么我写了KillInfo课程。如果Tuple实际上确实如此,那么您可以跳过我的课程并直接使用部分。