我正在尝试在带有循环的类函数内创建一个“矩阵/多维数组”。
(因为我已经习惯了其他语言)也无法使其正常工作。
有没有人可以帮助我完成这项工作,或者可以向我解释我做错了什么?
Class Matrix {
[int]$row
[int]$column
[int]$counter
$data
Matrix() {
Write-Host "No rows and columns provided"
}
Matrix($row, $column){
#Write-Host "row = " $row " column = " $column
$array = @(,@())
for ($i=0;$i -le $row -1 ; $i++) {
#Write-Host "This is row number " + $i
for ($j=0;$j -le $column -1 ; $j++) {
#Write-Host "This is column number " + $j
#00,01,02..10,11,12..
$array[$i][$j] = 2
}
}
$this.row = $row
$this.column = $column
$this.counter = 0
$this.data = $array
}
[void]add($adder){
if ($adder.GetType().name -eq "Matrix"){
Write-Host "You had given a matrix not a number"
}Else{
$this.counter = $this.counter + $adder
}
}
}
$matrix1 = [Matrix]::New(4,3)
$matrix1
答案 0 :(得分:0)
我明白了:) 对于正在搜索此内容的人,这是我使用的代码:
Matrix($row,$col){
$temp_array = New-Object 'object[,]' $row,$col #<<<<============
for ($i=0;$i -le $row -1 ; $i++) {
for ($j=0;$j -le $col -1 ; $j++) {
$temp_array[$i,$j] = Get-Random -Minimum 0 -Maximum 10
}
}
$this.row = $row
$this.col = $col
$this.counter = 0
$this.prosessed = 0
$this.data = $temp_array #<<<<============
}