문제

Does vb.net have a built-in function to check if a multidimensional array of unknown shape from a user passed as a Function parameter is jagged? If so, what is it?

도움이 되었습니까?

해결책

There are two issues here. Multidimensional arrays (defined like Integer(,)) are never "jagged". You can detect this by checking the array's Rank. The GetLowerBound and GetUpperBound methods can be used to check the length of the individual dimensions as needed.

If you are looking to see if a Jagged array (ie: Integer()()) is fully initialized, and each sub array is the same length (ie: determine whether a jagged array is "logically" rectangular), you could use something like:

Dim initializedToSameLengths As Boolean
If theArray(0) Is Nothing Then
   ' If first element is null, check for all others being null
   initializedToSameLengths = theArray.All(Sub(a) a = Nothing)
Else
   Dim firstLength = theArray(0).Length
   initializedToSameLengths = theArray.All(Sub(a) a <> Nothing AndAlso a.Length = firstLength)
End If

다른 팁

If the array is multidimensional, it will have more than 1 dimension (so the Array.Rank property will be >1). If the array is jagged, it will be a one-dimensional array with arrays as elements (so its Array.Rank property will be =1).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top