【发布时间】:2016-09-16 13:19:10
【问题描述】:
我正在向 listOF 添加元素,然后将该列表转换为数组。然后,我需要向每个数组添加特定数量的元素,同时还要保留每个元素中的数据。我很近,但还没有。
这不起作用。该数据保留在元素中,但代码并未将指定数量的元素添加到每个数组(每个数组需要 = 51)。
一如既往地感谢任何帮助。谢谢。
'Add an element to each ListOf(Integer) based on how many rows are in the DataGridView
For Each r As DataGridViewRow In dgvStepTest.Rows
accels.Add(r.Cells(0).Value) : decels.Add(r.Cells(1).Value) : Speeds.Add(r.Cells(2).Value)
holds.Add(r.Cells(3).Value) : flows.Add(r.Cells(4).Value) : Temps.Add(r.Cells(5).Value)
Next
'Convert each ListOf(Integer) to an Array
accels.TrimExcess() : accelRates = accels.ToArray
decels.TrimExcess() : decelRates = decels.ToArray
Speeds.TrimExcess() : spindleSpeeds = Speeds.ToArray
holds.TrimExcess() : holdTimes = holds.ToArray
flows.TrimExcess() : flowRates = flows.ToArray
Temps.TrimExcess() : oilTemps = Temps.ToArray
'Now determine the number of elements to add to each of the arrays so that the length of each array = 51
num = (51 - accelRates.Length)
'Now add the number of elements to each array based on the number calculated above, while also preserving the data
'already in each element in each of the arrays. New elements added should have values = 0.
Dim jaggedArray()() = New Integer(5)() {accelRates, decelRates, spindleSpeeds, holdTimes, flowRates, oilTemps}
For Each [Array] In jaggedArray
ReDim Preserve Array(Array.Length + num)
Next
【问题讨论】:
-
改用
Array.Resize()。还可以使用调试器单步执行您的代码以验证所有变量。 -
您是否有理由想要增加数组的大小(这很昂贵),而不是在将其转换为数组之前将额外的项目添加到您的列表中?
-
马克,感谢您的反馈。不,没有理由,因此我已经返回并更改了它,以便将额外的元素添加到列表中,然后转换为数组。所以,我正在完成我需要做的事情,即使它不符合最初的计划。不过,谢谢你的收获。话虽如此,我仍然想根据自己的知识制作原始作品,并且可能需要在某些时候使用它。
标签: arrays vb.net jagged-arrays