收录日期:2019/04/20 16:51:03 时间:2010-08-13 17:33:28 标签:vb.net,arrays,multidimensional-array

In vb.net how do you delete a row in a two dimensional array?

If you need to remove items from an array you probably shouldn't use an array but should be using a list of some kind (List(Of List(Of String)) or something.

If you do want to stick with the array, there's two different solutions described on this page, one slow shift everything by hand and one faster that copies the memory. The samples are for one dimensional arrays but should be fairly easy to adapt.

If it is the last row you want to remove and you are using the second dimension to represent rows, you can use ReDim with the preserve option like so:

Dim myArray(2,1)
ReDim Preserve myArray(2, 2)

Warning: I suggestion you check out this article before using the above example: The Redim Preserve Performance Trap

If you need to remove a row in the middle, you are going to have to shift everything down a row first, then truncate the last dimension of the array.

This coupled with the need to pivot your concept of rows to the second dimension probably makes it more trouble than it is worth. Chances are, you are using the wrong type in the first place if you need to arbitrarily delete items like that. Traditional arrays (especially multi-dimensional ones) are really best used for fixed-size data sets.