2016-04-14 74 views
0

我在我的二维字符串列表中使用.count时遇到问题。这是代码:二维字符串列表的长度不会返回正确的值

If File.Exists(fullPath) = True Then 
     Dim readText() As String = File.ReadAllLines(fullPath) 
     Dim s As String 
     accountCounter = 0 

     For Each s In readText 
      accountList.Add(New List(Of String)) 
      accountList.Add(New List(Of String)) 
      accountList.Add(New List(Of String)) 

      accountList(accountCounter).Add(s.Split(",")(0)) 
      accountList(accountCounter).Add(s.Split(",")(1)) 
      accountList(accountCounter).Add(s.Split(",")(2)) 
      accountCounter += 1 
     Next 
     print_logs(accountList.count) 
    End If 

结果是这样的:

{{姓名,电子邮件,密码},{姓名2,EMAIL2,密码2},{NAME3,EMAIL3,password3},{NAME4 ,EMAIL4,password4}}

怎么一回事,因为文件中有下面几行:
姓名,电子邮件,密码
名2,EMAIL2,密码2
NAME3,EMA il3,password3
name4,email4,password4

但数据不是问题,真正的问题是Count方法,它返回(12)。我认为,它返回4 * 3的结果,因为如果我添加此代码:

print_logs(accountList(0).Count)

它正确地返回3

所以,我怎么可以只返回4?

+0

@JamesThorpe,因为如果我像做了'因为我为整数= 0要accountList.Count - 1'他去从0到11 –

+0

' print_logs(accountCou也许)? – Arvo

+0

@Arvo我已经使用accountCounter,因为我没有解决方案,但是不可能(我认为)我无法返回我的列表行。 –

回答

2

在此代码创建三个新行每次你做一个迭代的......如果有四大行的文本文件,那么你将创建12 ...

而是执行此操作:

If File.Exists(fullPath) = True Then 
    Dim readText() As String = File.ReadAllLines(fullPath) 
    Dim s As String 
    accountCounter = 0 

    For Each s In readText 
     accountList.Add(New List(Of String)) 
     accountList(accountCounter).Add(s.Split(",")(0)) 
     accountList(accountCounter).Add(s.Split(",")(1)) 
     accountList(accountCounter).Add(s.Split(",")(2)) 
     accountCounter += 1 
    Next 
    print_logs(accountList.count) 
End If 

如果你想让它更好:

If File.Exists(fullPath) = True Then 
    Dim readText() As String = File.ReadAllLines(fullPath) 

    For Each s As String In readText 
     Dim newList = New List(Of String) 

     newList.Add(s.Split(",")(0)) 
     newList.Add(s.Split(",")(1)) 
     newList.Add(s.Split(",")(2)) 
     accountList.Add(newList) 
    Next 
    print_logs(accountList.count) 
End If