Newbie Question - Remove item from single-dimension array

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

Hi,

Just starting with vb.net and was wondering how I remove an item from an
integer array.

From my investigations I think I need the array.copy method (I believe this
will mean creating/calling a Function that returns a new array back into my
original array, minus the "deleted" item)

In my specific example I have a 32-item array and wish to remove item 5.

Am I aon the right track with the array.copy idea?

Any and all replies greatly appreciated.

Regards

Steven
 
Yes you are in the right track. You will create a new array then transfer
all the items minus the items you don't want.

Also my suggestion would be to use collections, in which it is easier to add
and remove items easily

VJ
 
* "Steven said:
Just starting with vb.net and was wondering how I remove an item from an
integer array.

That's not recommended in general.
From my investigations I think I need the array.copy method (I believe this
will mean creating/calling a Function that returns a new array back into my
original array, minus the "deleted" item)

In my specific example I have a 32-item array and wish to remove item 5.

Am I aon the right track with the array.copy idea?

This would work, but performance can be very bad.

Very quick and dirty:

\\\
Dim aint() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim i As Integer = Array.IndexOf(aint, 5)
If i >= 0 Then
Array.Copy(aint, i + 1, aint, i, aint.Length - i - 1)
ReDim Preserve aint(aint.Length - 2)
End If
Dim sb As New System.Text.StringBuilder
For Each i In aint
sb.Append(CStr(i) & ControlChars.NewLine)
Next i
MsgBox(sb.ToString())
///
 
Hi,

Thanks for both replies so far,

Herfried K. Wagner said:
That's not recommended in general.


This would work, but performance can be very bad.

Very quick and dirty:

\\\
Dim aint() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim i As Integer = Array.IndexOf(aint, 5)
If i >= 0 Then
Array.Copy(aint, i + 1, aint, i, aint.Length - i - 1)
ReDim Preserve aint(aint.Length - 2)
End If
Dim sb As New System.Text.StringBuilder
For Each i In aint
sb.Append(CStr(i) & ControlChars.NewLine)
Next i
MsgBox(sb.ToString())
///

This also works for me, but I'm guessing there's a good reason why I
shouldn't use it?

Function RemoveFromSingleDimensionArray(ByVal inArray() As Integer, ByVal
iRemoveItem As Integer) As Integer()
Dim narray() As Integer
Dim i As Integer, j As Integer
j = 0
ReDim narray(UBound(inArray) - 1)
For i = 0 To UBound(inArray) - 1
If i = iRemoveItem Then
j = j + 1
End If
narray(i) = inArray(j)
j = j + 1
Next
RemoveFromSingleDimensionArray = narray
End Function

Thanks again
Steven
 
Hi,

Thanks for both replies so far. The following solution I came up with also
works. Is there any reason why I shouldn't stick with this one?


Function RemoveFromSingleDimensionArray(ByVal inArray() As Integer, ByVal
iRemoveItem As Integer) As Integer()
Dim narray() As Integer
Dim i As Integer, j As Integer
j = 0
ReDim narray(UBound(inArray) - 1)
For i = 0 To UBound(inArray) - 1
If i = iRemoveItem Then
j = j + 1
End If
narray(i) = inArray(j)
j = j + 1
Next
RemoveFromSingleDimensionArray = narray
End Function

Thanks again
Steven



Herfried K. Wagner said:
That's not recommended in general.


This would work, but performance can be very bad.

Very quick and dirty:

\\\
Dim aint() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim i As Integer = Array.IndexOf(aint, 5)
If i >= 0 Then
Array.Copy(aint, i + 1, aint, i, aint.Length - i - 1)
ReDim Preserve aint(aint.Length - 2)
End If
Dim sb As New System.Text.StringBuilder
For Each i In aint
sb.Append(CStr(i) & ControlChars.NewLine)
Next i
MsgBox(sb.ToString())
///

This also works for me, but I'm guessing there's a good reason why I
shouldn't use it?
 
Hi,

Just starting with vb.net and was wondering how I remove an item from an
integer array.

In addition to the other responses, might I also suggest using an
ArrayList?

For example:

'Create the ArrayList
Dim a As New ArrayList

'Add 6 items
a.Add("Zero")
a.Add("One")
a.Add("Two")
a.Add("Three")
a.Add("Four")
a.Add("Five")
a.Add("Six")

'Now remove the item, "Three"
a.RemoveAt(3)


Remember that the indexing on an ArrayList starts at 0.
 
Back
Top