我正在嘗試構造一個字串陣列,但是如果我創建一個只有一個子陣列的陣列,它會以某種方式變成一個字符陣列?
但是,如果我傳入 2 個或多個硬編碼或來自變數的子陣列,它會按預期作業
我需要將它傳遞給一個自定義函式,然后我會對其進行迭代,但是由于它完全失敗的單個子陣列的奇怪方式
編輯:由于這顯然還不夠容易理解這里的更多資訊,還錯過了一些測驗代碼,其中包括我修復的 4 和 5 測驗。
如果您查看測驗 4a 和 4b,這就是單個字串子陣列的結果,您可以看到它沒有按預期作業,應該發生的是 4a 應該與 5a 相同 4b 應該創建一個索引界限例外。
這是我的測驗代碼:
$Maps = [string[]]::new(5)
$Maps[0] = 'Ireland'
$Maps[1] = 'Scotland'
$Maps[2] = 'England'
$Maps[3] = 'Germany'
$Maps[4] = 'France'
$Maps2 = [string[]]::new(5)
$Maps2[0] = 'Japan'
$Maps2[1] = 'Crete'
$Maps2[2] = 'USA'
$Maps2[3] = 'Canada'
$Maps2[4] = 'Greece'
$Array = @($Maps)
Write-Host 'These results seem to treat a single variable as character arrays?'
Write-Host Test 1a: $Array[0][0]
Write-Host Test 1a: $Array[0][1]
Write-Host Test 1b: $Array[1][0]
Write-Host Test 1b: $Array[1][1]
Write-Host Test 1c: $Array[2][0]
Write-Host Test 1c: $Array[2][1]
$Array = @($Maps, $Maps2)
Write-Host 'These results seem to create the correct results'
Write-Host Test 2a: $Array[0][0]
Write-Host Test 2a: $Array[0][1]
Write-Host Test 2b: $Array[1][0]
Write-Host Test 2b: $Array[1][1]
Write-Host Test 2c: $Array[2][0]
Write-Host Test 2c: $Array[2][1]
$Array = @($Maps, @('Test1', 'test2'))
Write-Host 'These results seem to create the correct results'
Write-Host Test 3b: $Array[0][0]
Write-Host Test 3b: $Array[0][1]
Write-Host Test 3c: $Array[1][0]
Write-Host Test 3c: $Array[1][1]
Write-Host Test 3d: $Array[2][0]
Write-Host Test 3d: $Array[2][1]
$Array = @(@('Available Maps', 'Scotland', 'England', 'Germany', 'France'))
Write-Host 'Same Issue as First Example'
Write-Host Test 4a: $Array[0][0]
Write-Host Test 4a: $Array[0][1]
Write-Host Test 4b: $Array[1][0]
Write-Host Test 4b: $Array[1][1]
$Array = @(@('Available Maps', 'Scotland', 'England', 'Germany', 'France'), @('Test1', 'test2'))
Write-Host 'Works as Expected'
Write-Host Test 5a: $Array[0][0]
Write-Host Test 5a: $Array[0][1]
Write-Host Test 5b: $Array[1][0]
Write-Host Test 5b: $Array[1][1]
這是結果
These results seem to treat a single variable as character arrays?
Test 1a: I
Test 1a: r
Test 1b: S
Test 1b: c
Test 1c: E
Test 1c: n
These results seem to create the correct results
Test 2a: Ireland
Test 2a: Scotland
Test 2b: Japan
Test 2b: Crete
Cannot index into a null array.
At line:34 char:5
Write-Host Test 2c: $Array[2][0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At line:35 char:5
Write-Host Test 2c: $Array[2][1]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : NullArray
These results seem to create the correct results
Test 3b: Ireland
Test 3b: Scotland
Test 3c: Test1
Test 3c: test2
Cannot index into a null array.
At line:45 char:5
Write-Host Test 3d: $Array[2][0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At line:46 char:5
Write-Host Test 3d: $Array[2][1]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : NullArray
Same Issue as First Example
Test 4a: A
Test 4a: v
Test 4b: S
Test 4b: c
Works as Expected
Test 5a: Available Maps
Test 5a: Scotland
Test 5b: Test1
Test 5b: test2
謝謝
uj5u.com熱心網友回復:
在示例中4a,4b如果您想要一個包含2 個嵌套陣列的陣列,您可以使用內部空陣列子運算式運算子@():
$Array = @(@(), @('Available Maps', 'Scotland', 'England', 'Germany', 'France'))
$Array[0] # => Is an empty `System.Array`
$Array[1] # => Is the populated `System.Array`
$Array[1][0] # => Available Maps
如果您希望上面的示例拋出越界例外,您可以$null改用:
$Array = @($null, @('Available Maps', 'Scotland', 'England', 'Germany', 'France'))
$Array[0] # => Is `$null`
$Array[0][0] # => Is Out of Bounds
$Array[1] # => Is the populated `System.Array`
$Array[1][0] # => Available Maps
使用逗號運算子,,您可以在父陣列的索引0處創建嵌套陣列:
$Array = @(, @('Available Maps', 'Scotland', 'England', 'Germany', 'France'))
$Array[0] # => Is the populated `System.Array`
$Array[0][0] # => Available Maps
$Array[1][0] # => Is Out of Bounds
請注意,在所有這些示例中,@()都不需要父級,PowerShell 可以為您動態假設:
$Array = @(), @('Available Maps', 'Scotland', 'England', 'Germany', 'France')
$Array = $null, @('Available Maps', 'Scotland', 'England', 'Germany', 'France')
$Array = , @('Available Maps', 'Scotland', 'England', 'Germany', 'France')
作為個人意見,我發現哈希表更適合于此。例如,第一個代碼片段是:
$hash = @{
Array0 = @()
Array1 = 'Available Maps', 'Scotland', 'England', 'Germany', 'France'
}
$hash['array0'] # => Is an empty `System.Array`
$hash['array1'] # => Is the populated `System.Array`
$hash['array1'][0] # => Available Maps
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444144.html
