simple question about arrays

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

Dear all,
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????
...
 
Boni said:
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????

You'll have to create a new array of appropriate size and copy over the
items of the original array to the new array using 'Array.Copy' or
'Array.CopyTo'.
 
It is ugly. Is there no better solution? If split(.) returns an array I
can't take other collection type. Is it really nothing more appropriate?
 
You can't 'delete' an element from the middle of an array. You need to use a
collection for that functionality, or run through your array, copying all non
blank elements into another array.
 
Boni said:
It is ugly. Is there no better solution? If split(.) returns an
array I can't take other collection type. Is it really nothing more
appropriate?


In addition the other answers: You can't delete an item because you would
have to quarry out a piece of your RAM module. Sounds funny but that's how
it is. Just like you can't delete a line from a text file on your HD. You
have to overwrite it by the subsequent lines (or array elements). As the
length of an array is fixed, there would be one remaining ununsed item at
the end. To solve the problem, the whole array must be copied into a new
array - without the item to be deleted. The same is done by Redim Preserve
BTW.


Armin
 
In addition the other answers: You can't delete an item because you would
have to quarry out a piece of your RAM module
Was it a joke? I did it and my PC is dead now! :)
 
Boni said:
Dear all,
I am wondering, that I can't find how to delete an element from an array.

I'd suggest constucting an ArrayList from the String array :

Dim a As New ArrayList( SomeString.Split(","c) )

For iIdx as Integer = a.Count - 1 To 0 Step -1
If False Then
a.RemoveAt( iIdx )
End if
Next

HTH,
Phill W.
 
Boni said:
Dear all,
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????
..

There is another option, you can use arraylist to do the removing for you.

dim Str() as string
dim Arr as new ArrayList(str.split(","))

for ii as integer = 0 to Arr.Count-1
if cstr(Arr(ii)).length = 0 then Arr.Remove(ii)
Next

Chris
 
I think you would find it quicker to dim another array the same size of array
"a" then copy only the non blank elements to the new array and count them.
Then redim preserve the new array to the count of non items, i.e.

dim b(a.upperbound) as string

dim count as integer =0
for i as integer= 0 to a.upperbound
if a(i) <>"" then
b(count)=a(i)
count = count+1
end if
next

redim preserve b(count-1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top