By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Delete element from a 2-D array

P: n/a
Hi,

Is there easy way to delete elements from a 2D array?
Thanks for your tip.

Hugh
Nov 20 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
* "Hugh" <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
"Hugh" <an*******@discussions.microsoft.com> schrieb

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...ollections.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.

--
Armin

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

Nov 20 '05 #3

P: n/a
Cor
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
Is there easy way to delete elements from a 2D array?
Thanks for your tip.

Hugh

Nov 20 '05 #4

P: n/a
* "Cor" <no*@non.com> scripsit:
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"?

;-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
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

Nov 20 '05 #6

P: n/a
Cor
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
////
Nov 20 '05 #7

P: n/a
Cor
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
///
Nov 20 '05 #8

P: n/a
Cor
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
Nov 20 '05 #9

P: n/a
* "Cor" <no*@non.com> scripsit:
The example is something larger


That's IMO the main "problem". There is no "easy" one-line-solution.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #10

P: n/a
Cor
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.
That's IMO the main "problem". There is no "easy" one-line-solution.

Nov 20 '05 #11

P: n/a
"Hugh" <an*******@discussions.microsoft.com> schrieb
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.
--
Armin

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

Nov 20 '05 #12

P: n/a
Cor
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
Nov 20 '05 #13

P: n/a
* "Cor" <no*@non.com> scripsit:
Did you look at my arraylist sample?


Yes.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #14

P: n/a
-----Original Message-----
"Hugh" <an*******@discussions.microsoft.com> schrieb
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 thesize. Redim Preserve also creates a new array, but it might be the simplestsolution. Nevertheless, Redim Preserve can only change the size of thehighest rank.
</quote>

If you have specific problems to implement this solution, please let usknow.
--
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
Nov 20 '05 #15

P: n/a
-----Original Message-----
Hi Hugh,

I was busy with the homework from Herfried (is only joking) I changed alittle 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
Nov 20 '05 #16

P: n/a
<an*******@discussions.microsoft.com> schrieb

<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.


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)
--
Armin

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

Nov 20 '05 #17

P: n/a
Cor
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
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

Nov 20 '05 #18

P: n/a
Cor
Hi Herfried,

So much work with your homework and then such a short answer

:-(

:-))))))

Cor
Yes.

Nov 20 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.