2016-04-14 72 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