Delete element from a 2-D array

  • Thread starter Thread starter Hugh
  • Start date Start date
* "Hugh said:
Is there easy way to delete elements from a 2D array?

Set it to a specific value. What do you mean with "delete"? Please
provide a simple sample of what you want to do.
 
Hugh said:
Is there easy way to delete elements from a 2D array?

This is not possible because the length (= size) of an array is fixed. You'd
have to copy the array to a new array without the item to be removed. Or,
use an arraylist or any other sort of collection. More info:

http://msdn.microsoft.com/library/en-us/cpguide/html/cpcongroupingdataincollections.asp

Another option is to copy the items within the array to the new position
(overwriting the item to be removed), then use Redim Preserve to change the
size. Redim Preserve also creates a new array, but it might be the simplest
solution. Nevertheless, Redim Preserve can only change the size of the
highest rank.
 
Hi Hugh,

If the 2d array is a datatable or an arraylist, than it is easy possible

using remove, removeat with arraylist (or other implementations of Ilist
arrays)

using delete,deleteat,remove, removeat with the datatable

I hope this helps?

Cor
 
* "Cor said:
If the 2d array is a datatable or an arraylist, than it is easy possible

using remove, removeat with arraylist (or other implementations of Ilist
arrays)

1, 2, 4, 5
1, 1, 1, 5
3, 4, 9, 1
1, 1, 1, 1

How to delete the "9"?

;-)
 
Thank you all for your reply. I am sorry that original
post was misleading. It should be "delete a row" not a
element. Let's say, I have a 2D array:

0,1
2,3
4,5
6,7
8,9
10,11
12,13
14,15

and I want to delete row 3 from the array. What is the
easiest way to do it? Thanks again.

Hugh
 
Hi Herfried,

Normaly we are not helping with homework, but because it is you and you
know.

But I apriciate an answer if I help you

Is this what what you needed?

:-))))

Cor
1, 2, 4, 5
1, 1, 1, 5
3, 4, 9, 1
1, 1, 1, 1
How to delete the "9"?
\\\
Public Module Main
Sub main()
Dim Hkw(,) As Integer = {{1, 2, 3, 4}, _
{1, 1, 1, 5}, {3, 4, 9, 1}, {1, 1, 1, 1}}
Dim R As New ArrayList
For i As Integer = 0 To 3
Dim C As New ArrayList
For y As Integer = 0 To 3
c.Add(Hkw(i, y))
Next
R.Add(c)
Next
MessageBox.Show(DirectCast(R(2), ArrayList)(2).ToString)
DirectCast(R(2), ArrayList).RemoveAt(2)
MessageBox.Show(DirectCast(R(2), ArrayList)(2).ToString)
End Sub
////
 
Hi Hugh,

I was busy with the homework from Herfried (is only joking) I changed a
little to make it more viewable for you but not exact as your example.

I hope this helps,

Cor
\\\
Public Module Main
Sub main()
Dim Hkw(,) As Integer = {{1, 2, 3, 4}, _
{1, 1, 1, 5}, {3, 4, 9, 1}, {1, 1, 1, 1}}
Dim R As New ArrayList
For i As Integer = 0 To 3
Dim C As New System.Text.StringBuilder
For y As Integer = 0 To 3
C.Append(Hkw(i, y))
Next
R.Add(c)
Next
MessageBox.Show(R(2).ToString)
R.RemoveAt(2)
MessageBox.Show(R(2).ToString)
End Sub
///
 
Hi Herfried,

I forgot,

I can do it with a dataset also, but maybe you believe that?

The example is something larger, however in a xml dataset you can delete
elements and put them on nothing.

Cor
 
Hugh said:
Thank you all for your reply. I am sorry that original
post was misleading. It should be "delete a row" not a
element. Let's say, I have a 2D array:

0,1
2,3
4,5
6,7
8,9
10,11
12,13
14,15

and I want to delete row 3 from the array. What is the
easiest way to do it? Thanks again.


<quote>
Another option is to copy the items within the array to the new position
(overwriting the item to be removed), then use Redim Preserve to change the
size. Redim Preserve also creates a new array, but it might be the simplest
solution. Nevertheless, Redim Preserve can only change the size of the
highest rank.
</quote>

If you have specific problems to implement this solution, please let us
know.
 
Did you look at my arraylist sample?

When I become angry (not real) than I show you it with the dataset also, but
first the arraylist above this line.
 
Hi Herfried,

It can be because my variable quoting style that you did not see it (For
Armin in an other way than for others and with you variable)

This is a repeat from a previous message.
\\\
Public Module Main
Sub main()
Dim Hkw(,) As Integer = {{1, 2, 3, 4}, _
{1, 1, 1, 5}, {3, 4, 9, 1}, {1, 1, 1, 1}}
Dim R As New ArrayList
For i As Integer = 0 To 3
Dim C As New ArrayList
For y As Integer = 0 To 3
c.Add(Hkw(i, y))
Next
R.Add(c)
Next
MessageBox.Show(DirectCast(R(2), ArrayList)(2).ToString)
DirectCast(R(2), ArrayList).RemoveAt(2)
MessageBox.Show(DirectCast(R(2), ArrayList)(2).ToString)
End Sub
////

Cor
 
-----Original Message-----



<quote>
Another option is to copy the items within the array to the new position
(overwriting the item to be removed), then use Redim Preserve to change the
size. Redim Preserve also creates a new array, but it might be the simplest
solution. Nevertheless, Redim Preserve can only change the size of the
highest rank.
</quote>

If you have specific problems to implement this solution, please let us
know.


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.
Hi Armin,

I think that I understand your idea. You meant to
redefine the array by excluding specified row(s) by using
Redim Preserve statement. Let me know if that is not what
you said. Thanks.

Hugh
 
-----Original Message-----
Hi Hugh,

I was busy with the homework from Herfried (is only joking) I changed a
little to make it more viewable for you but not exact as your example.

I hope this helps,

Cor
\\\
Public Module Main
Sub main()
Dim Hkw(,) As Integer = {{1, 2, 3, 4}, _
{1, 1, 1, 5}, {3, 4, 9, 1}, {1, 1, 1, 1}}
Dim R As New ArrayList
For i As Integer = 0 To 3
Dim C As New System.Text.StringBuilder
For y As Integer = 0 To 3
C.Append(Hkw(i, y))
Next
R.Add(c)
Next
MessageBox.Show(R(2).ToString)
R.RemoveAt(2)
MessageBox.Show(R(2).ToString)
End Sub
///
Thank you very much, Cor, for your sample code. I never
used Arraylist before. Your sample code is very eye
opening. Is this the approach only works with arraylist
not array. Sorry for late reply. I do not sit in my desk
most of the time. Thanks again.

Hugh
 
I think that I understand your idea. You meant to
redefine the array by excluding specified row(s) by using
Redim Preserve statement. Let me know if that is not what
you said. Thanks.

Something like this:

Dim s(2, 3) As String
Dim i, j As Integer

'fill array

For i = 0 To 2
For j = 0 To 3
s(i, j) = i & "," & j
Next
Next

'delete row #1

For i = 0 To 2
For j = 1 To 2
s(i, j) = s(i, j + 1)
Next
Next

ReDim Preserve s(2, 2)
 
Hi Hugh,

Have a look for the keywords "Ilist" you find than a lot of arrays which
derive from that arraylist.
There you can see which have the removeat and remove.

You can also make your own array from that.

I hope this helps?

Cor
 
Back
Top