2012-06-24 19 views
1

如何在vb.net中定義,動態初始化和打印3D數組?vb.net中的3D數組

我有這個代碼返回一個3d數組,我有錯誤告訴我有一個參數超出範圍..當我尋找線我發現沒有錯?

這是函數的代碼:

Private Function readFESTWERTEBLOCK(ByVal FESTWERTEBLOCKString As String) _ 
    As String (,,) 

    Dim allX As System.Text.RegularExpressions .Group 
    Dim allY As System.Text.RegularExpressions.Group 
    Dim allZ As System.Text.RegularExpressions.Group 
    Dim tempArray (,,) As String 
    Dim allXval As System.Text.RegularExpressions .MatchCollection 
    Dim allYval As System.Text.RegularExpressions.MatchCollection 
    Dim allZval As System.Text.RegularExpressions.MatchCollection 
    Dim oldUp ,midUp,newUp As Integer 
    Dim a,b,c 
    Dim myRegExpallX As New System.Text.RegularExpressions.Regex("WERT([\-\s\.0-9e]+)") 
    Dim myRegExpallY As New System.Text.RegularExpressions.Regex("WERT([\-\s\.0-9e]+)") 
    Dim myRegExpallZ As New System.Text.RegularExpressions.Regex("WERT([\-\s\.0-9e]+)") 
    Dim myRegExpallXval As New System.Text.RegularExpressions.Regex("[\w\-\.]+") 
    Dim myRegExpallYval As New System.Text.RegularExpressions.Regex("[\w\-\.]+") 
    Dim myRegExpallZval As New System.Text.RegularExpressions.Regex("[\w\-\.]+") 

    ReDim tempArray (0 To 2 ,0 To -1, 0 To -1) 
    For a=0 To myRegExpallX.Matches(FESTWERTEBLOCKString).Count-1 
     If myRegExpallX.Matches(FESTWERTEBLOCKString).Item(a).Groups.Count>1 And myRegExpallY.Matches(FESTWERTEBLOCKString).item(a).Groups.Count>1 AndAlso myRegExpallZ.Matches(FESTWERTEBLOCKString).item(a).Groups.Count>1 Then 
      allX = myRegExpallX.Matches(FESTWERTEBLOCKString).Item(a).Groups.Item(1) 
      allXval= myRegExpallXval.Matches(allX.Value.ToString()) 

      allY = myRegExpallY.Matches(FESTWERTEBLOCKString).Item(a).Groups.Item(1) 
      allYval= myRegExpallYval.Matches(allY.Value.ToString()) 

      allZ = myRegExpallZ.Matches(FESTWERTEBLOCKString).Item(a).Groups.Item(1) 
      allZval= myRegExpallZval.Matches(allZ.Value.ToString()) 

      oldUp= UBound(tempArray,3) 
      midUp= UBound(tempArray , 3) + allXval.Count 
      newUp=UBound(tempArray,3)+allXval.Count+allYval.Count 

      ReDim Preserve tempArray(0 To 2 ,0 To midUp ,0 To newUp) 
      For c=oldUp+2 To (allXval.Count+allYval.Count+oldUp) 
       For b= midUp+2 To allXval.Count+midUp 
        Dim tmpMatchX As System.Text.RegularExpressions.Match=allXval.Item(b-oldUp-2) 
        tempArray(0,b,c)=tmpMatchX.Value.ToString() 
        Dim tmpMatchY As System.Text.RegularExpressions.Match=allYval.Item(b-oldUp-2) 
        tempArray(1,b,c)=tmpMatchY.Value.ToString() 
        Dim tmpMatchZ As System.Text.RegularExpressions.Match=allZval.Item(b-oldUp-2) 
        tempArray(2,b,c)=tmpMatchZ.Value.ToString() 
       Next 
      Next 
     End If 
    Next  
    readFESTWERTEBLOCK = tempArray 
End Function 
+1

你誤以爲這個網站的「調試的代碼爲我的」服務檯。您正在編寫上個世紀的代碼。用List(Of T)重寫它,所以你不再需要這些可怕的Redim語句。當你無法做*那*工作時回來。 –

回答

3

可以初始化這樣

Dim a(,,) As Double 

a = New Double(5, 4, 10) {} 

此陣列的3維陣列具有以下指數範圍

a(0..5, 0..4, 0..10) 

並具有大小爲6 x 5 x 11個元素。


如果您有尺寸,可以長,使用List(Of T)他們

Dim tempArray As List(Of List(Of String))() = New List(Of List(Of String))(2) {} 

tempArray(0) = New List(Of List(Of String))() 

tempArray(0).Add(New List(Of String)()); 
tempArray(0)(0).Add("Some string"); 
tempArray(0)(0).Add("Some other string"); 

tempArray(0).Add(New List(Of String)()); 
tempArray(0)(1).Add("XXX"); 
tempArray(0)(1).Add("YYY"); 
tempArray(0)(1).Add("ZZZ"); 

tempArray(0)(1)(2) ===> "YYY" 
+0

感謝您的回覆;我實際上是在一個預先製作的軟件中工作,這就是爲什麼我有很多錯誤,一旦我需要添加一個功能..謝謝你,我會回來,如果我不能讓它工作 –