Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 我有两个数组和一个文本文件,我希望它将一个特定的单词放入一个数组,将其他单词放入另一个数组(VB)_Arrays_Vb.net_Streamreader - Fatal编程技术网

Arrays 我有两个数组和一个文本文件,我希望它将一个特定的单词放入一个数组,将其他单词放入另一个数组(VB)

Arrays 我有两个数组和一个文本文件,我希望它将一个特定的单词放入一个数组,将其他单词放入另一个数组(VB),arrays,vb.net,streamreader,Arrays,Vb.net,Streamreader,所以我在visual basic中遇到了一个问题,我有两个数组,分别称为arrLang1和arrLang2 我想把-前面的单词放到第一个数组中,把-后面的单词放到第二个数组中。这些单词来自一个txt文件 瑞典语 瑞典语2-英语单词2 瑞典语-英语单词 瑞典语-英语单词 你为什么不改用a呢?它们是专门为这种需求而创建的 字典泛型类提供了从一组键到一组值的映射。字典中的每个添加项都包含一个值及其关联的键。使用键检索值的速度非常快,接近O(1)。每个键都必须是唯一的 Dim allLines = Fr

所以我在visual basic中遇到了一个问题,我有两个数组,分别称为arrLang1和arrLang2

我想把-前面的单词放到第一个数组中,把-后面的单词放到第二个数组中。这些单词来自一个txt文件

瑞典语

瑞典语2-英语单词2

瑞典语-英语单词

瑞典语-英语单词

你为什么不改用a呢?它们是专门为这种需求而创建的

字典
泛型类提供了从一组键到一组值的映射。字典中的每个添加项都包含一个值及其关联的键。使用键检索值的速度非常快,接近O(1)。每个键都必须是唯一的

Dim allLines = From line In File.ReadLines(path) Where Not String.IsNullOrWhiteSpace(line)
Dim dict = New Dictionary(Of String, String)
For Each line As String In allLines
    Dim words = line.Split({" - "}, StringSplitOptions.RemoveEmptyEntries)
    If words.Length >= 2 Then
        dict(words(0)) = words(1)
    End If
Next
如果您坚持使用集合,我会使用
列表(字符串)
而不是数组,因为您不知道正确的大小,数组的大小是固定的:

Dim swedishWords = New List(Of String)
Dim englishWords = New List(Of String)
For Each line As String In allLines
    Dim words = line.Split({" - "}, StringSplitOptions.RemoveEmptyEntries)
    If words.Length >= 2 Then
        swedishWords.Add(words(0))
        englishWords.Add(words(1))
    End If
Next
如果您真的需要数组
swedishWords.ToArray()
englishWords.ToArray()