将数组分配给交错数组

时间:2021-06-15 21:20:04

Newby here.

纽比。

I want to assign an array to an array of arrays (or jagged array).

我想给数组(或交错数组)分配一个数组。

I am doing the following:

我正在做以下事情:

    Dim arrOutputData As Double() = New Double(0) {}
    Dim arrOutputArray As Double()() = New Double(9)() {}

    For intStepping As Integer = 0 To 9
        arrOutputData(0) = intStepping
        arrOutputArray(intStepping) = arrOutputData
    Next intStepping

What I want is that arrOutputArray(0)(0)=0 on the first iteration, then arrOutputArray(1)(0)=1 on the second iteration, arrayOutputArray(2)(0)=2 and so forth.

我想要的是arrOutputArray(0)(0)=0在第一个迭代上,然后arrOutputArray(1)(0)=1在第二个迭代上,arrayOutputArray(2)(0)=2,以此类推。

What's actually happening is that arrOutputArray(0)(0)=0 on the first iteration, but the instant arrOutputData(0) is assigned 1 on the second iteration, arrOutputArray(0)(0) now equals 1 without even arriving at the line where arrOutputArray(1) = arrOutputData.

实际上,在第一次迭代中arrOutputArray(0)(0)=0,但是在第二次迭代中,即时的arrOutputArray(0)被赋值为1,arrOutputArray(0)(0)现在等于1,甚至连arrOutputArray(1) = arrOutputData的那一行都没有。

Tried this on vb.net 2008 and 2015 (on different computers) and I get the same result.

在vb.net 2008和2015(在不同的计算机上)尝试过这个,我得到了相同的结果。

What am I doing wrong???

我做错了什么???

What bewilders me is that when the code reaches and executes arrOutputData(0)=1, immediately arrOutputArray(0)(0)=1. When the code reaches and executes the next line arrOutputArray(1)=arrOutputData, both arrOutputArray(0)(0) and arrOutputArray(1)(0) are now equal to 1.

令我困惑的是,当代码到达并执行arrOutputData(0)=1时,arrOutputArray(0)(0)=1。当代码到达并执行下一行arrOutputArray(1)=arrOutputData时,arrOutputArray(0)(0)和arrOutputArray(1)(0)现在都等于1。

Tried changing declaration syntax so Redim Preserve works, and still the same behaviour...

尝试改变声明语法以便重拨保存工作,并且仍然保持相同的行为……

What gives?

到底发生了什么事?

Thanks!

谢谢!

Edit: I tried new code:

编辑:我尝试了新代码:

    Dim arrInputData As Double() = New Double(intGlobalHistory) {}
    Dim arrOutputData As Double() = New Double(0) {}

    Dim arrInputArray As Double()() = New Double(intGlobalStepping)() {}
    Dim arrOutputArray As Double()() = New Double(intGlobalStepping)() {}

    For intStepping As Integer = 0 To intGlobalStepping
        For intHistory As Integer = (intGlobalHistory + 1) To 1 Step -1
            arrInputData(intHistory - 1) = dblSomeInputFactor + CDbl(intHistory)
        Next intHistory

        arrOutputData = New Double() {dblSomeOutputFactor + CDbl(intStepping)}

        arrInputArray(intStepping) = arrInputData
        arrOutputArray(intStepping) = arrOutputData
    Next intStepping

While now the arrOutputData and arrOutputArray variables work great, the arrInputData and arrInputArray don't, in the sense that it's behaving like described in the first part of the question. Any ideas?

现在arrOutputData和arrOutputArray变量工作得很好,arrInputData和arrInputArray则没有,从某种意义上说,它的行为就像问题的第一部分所描述的那样。什么好主意吗?

I feel like I must change something here to include the New keyword somehow:

我觉得我必须改变一些东西,以某种方式包含新的关键字:

   arrInputData(intHistory - 1) = dblSomeInputFactor + CDbl(intHistory)

just not sure how or where exactly...

只是不知道具体如何……

1 个解决方案

#1


0  

You need to change this

你需要改变这个

    arrOutputData(0) = intStepping

to this

这个

    arrOutputData = New Double() {intStepping}

or more compressed like this (skipping the "arrOutputData" variable)

或者更像这样压缩(跳过“arrOutputData”变量)

    arrOutputArray(intStepping) = New Double() {intStepping}

Instead of looping one can also assign values at creation time like this

在创建时,可以像这样分配值,而不是循环

    Dim arrOutputArray As Double()() = New Double()() {New Double() {1, 2, 3}, New Double() {4, 5, 6}, New Double() {7, 8, 9}}

which will give you three items in the arrOutputArray, each holding a three
item array

arrOutputArray中有三个项,每个项都包含三个项数组

You can also assign a value array to a specific index of the arrOutputArray like this

您还可以像这样为arrOutputArray的特定索引分配值数组

    arrOutputArray(3) = New Double() {22, 33, 44}
    or
    arrOutputData = New Double() {22, 33, 44}
    arrOutputArray(3) = arrOutputData

just make sure the index exist or an exception will occur.

只要确保索引存在,否则就会发生异常。

Check this link: The difference between value types and reference types

检查这个链接:值类型和引用类型之间的区别

#1


0  

You need to change this

你需要改变这个

    arrOutputData(0) = intStepping

to this

这个

    arrOutputData = New Double() {intStepping}

or more compressed like this (skipping the "arrOutputData" variable)

或者更像这样压缩(跳过“arrOutputData”变量)

    arrOutputArray(intStepping) = New Double() {intStepping}

Instead of looping one can also assign values at creation time like this

在创建时,可以像这样分配值,而不是循环

    Dim arrOutputArray As Double()() = New Double()() {New Double() {1, 2, 3}, New Double() {4, 5, 6}, New Double() {7, 8, 9}}

which will give you three items in the arrOutputArray, each holding a three
item array

arrOutputArray中有三个项,每个项都包含三个项数组

You can also assign a value array to a specific index of the arrOutputArray like this

您还可以像这样为arrOutputArray的特定索引分配值数组

    arrOutputArray(3) = New Double() {22, 33, 44}
    or
    arrOutputData = New Double() {22, 33, 44}
    arrOutputArray(3) = arrOutputData

just make sure the index exist or an exception will occur.

只要确保索引存在,否则就会发生异常。

Check this link: The difference between value types and reference types

检查这个链接:值类型和引用类型之间的区别