从多维数组中删除Nothing值



我怎样才能删除具有"无"的值并将数组减少到具有值的值?

假设我有一个叫做modvaluestmp(,)的二维数组,我使用了下面的linq语句来删除任何东西。它工作,但结果数组(结果)是一个单维数组。有人能帮我返回一个二维数组保存索引吗?

Dim result() = (From str In modvaluestmp
              Where Not {Nothing}.Contains(str)).ToArray()

现有的数组是这样的

modvaluestmp(0,0) = "sometext1"
modvaluestmp(0,1) = "sometext2"
modvaluestmp(0,2) = "sometext3"
modvaluestmp(0,3) = "sometext4"
modvaluestmp(0,4) = Nothing
modvaluestmp(0,5) = Nothing
modvaluestmp(1,0) = "sometext5"
modvaluestmp(1,1) = "sometext6"
modvaluestmp(1,2) = "sometext7"
modvaluestmp(1,3) = "sometext8"
modvaluestmp(1,4) = Nothing
modvaluestmp(1,5) = Nothing

我希望数组如下

modvaluestmp(0,0) = "sometext1"
modvaluestmp(0,1) = "sometext2"
modvaluestmp(0,2) = "sometext3"
modvaluestmp(0,3) = "sometext4"
modvaluestmp(1,0) = "sometext5"
modvaluestmp(1,1) = "sometext6"
modvaluestmp(1,2) = "sometext7"
modvaluestmp(1,3) = "sometext8"

你应该使用锯齿数组而不是正方形的2D数组。所以你的代码应该像这样:

 Dim modvaluestmp = New String(1)() {}
 modvaluestmp(0) = New String(5) {}
 modvaluestmp(1) = New String(5) {}
 modvaluestmp(0)(0) = "sometext1"
 modvaluestmp(0)(1) = "sometext2"
 modvaluestmp(0)(2) = "sometext3"
 modvaluestmp(0)(3) = "sometext4"
 modvaluestmp(0)(4) = Nothing
 modvaluestmp(0)(5) = Nothing
 modvaluestmp(1)(0) = "sometext5"
 modvaluestmp(1)(1) = "sometext6"
 modvaluestmp(1)(2) = "sometext7"
 modvaluestmp(1)(3) = "sometext8"
 modvaluestmp(1)(4) = Nothing
 modvaluestmp(1)(5) = Nothing
 Dim result = (From ary In modvaluestmp Select ary.Where(Function(str) Not str Is Nothing).ToArray()).ToArray()

帮助链接:

维基百科中的锯齿数组

假设:

  1. 数组中只有尾随空元素
  2. 你想截断所有的行,使他们有相同数量的元素(可能丢失数据)
  3. 每一行至少有一个元素

然后您可以确定哪一行是最小的,并使用ReDim Preserve:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim modvaluestmp(,) As String = {
        {"sometext1", "sometext2", "sometext3", "sometext4", Nothing, Nothing},
        {"sometext5", "sometext6", "sometext7", "sometext8", Nothing, Nothing},
        {"sometext9", "sometext10", Nothing, Nothing, Nothing, Nothing}
    }
    Console.WriteLine(DisplayArray(modvaluestmp))
    Dim max As Integer = modvaluestmp.GetUpperBound(1)
    For y As Integer = 0 To modvaluestmp.GetUpperBound(0)
        For x As Integer = modvaluestmp.GetUpperBound(1) To 0 Step -1
            If Not IsNothing(modvaluestmp(y, x)) AndAlso x < max Then
                max = x
                Exit For
            End If
        Next
    Next
    ReDim Preserve modvaluestmp(modvaluestmp.GetUpperBound(0), max)
    Console.WriteLine(DisplayArray(modvaluestmp))
End Sub
Private Function DisplayArray(ByVal arr(,) As String) As String
    Dim sb As New System.Text.StringBuilder
    For y As Integer = 0 To arr.GetUpperBound(0)
        For x As Integer = 0 To arr.GetUpperBound(1)
            sb.Append(If(x = 0, "", ", ") & arr(y, x))
        Next
        sb.AppendLine("")
    Next
    Return sb.ToString
End Function
输出:

sometext1, sometext2, sometext3, sometext4, , 
sometext5, sometext6, sometext7, sometext8, , 
sometext9, sometext10, , , , 
sometext1, sometext2
sometext5, sometext6
sometext9, sometext10

---------编辑----------

下面是一个使用List(Of List(Of String)的例子:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim modvaluestmp(,) As String = {
        {"sometext1", "sometext2", "sometext3", "sometext4", Nothing, Nothing},
        {"sometext5", "sometext6", "sometext7", "sometext8", Nothing, Nothing},
        {"sometext9", "sometext10", Nothing, Nothing, Nothing, Nothing},
        {"sometext11", "sometext12", "sometext13", Nothing, Nothing, Nothing}
    }
    Dim stamps As New List(Of List(Of String))
    For y As Integer = 0 To modvaluestmp.GetUpperBound(0)
        Dim newRow As New List(Of String)
        For x As Integer = 0 To modvaluestmp.GetUpperBound(1)
            If Not IsNothing(modvaluestmp(y, x)) Then
                newRow.Add(modvaluestmp(y, x))
            End If
        Next
        stamps.Add(newRow)
    Next
    For Each row As List(Of String) In stamps
        Console.WriteLine(String.Join(", ", row))
    Next
    ' Example of accessing an element directly:
    Console.WriteLine("stamps(0)(2) = " & stamps(0)(2))
End Sub

相关内容

  • 没有找到相关文章

最新更新