Saturday, March 31, 2012

Trim array of null object

How to remove null objects from an array? When I use the split function, I end up having an empty first element. How do I remove this? Any code is welcome...
ArrayList alArray = new ArrayList();

for ( int i = 0; i < myArray.Length; ++i )
{
if ( myArray[i] != null )
alArray.Add(myArray[i]);
}


Or maybe, if you want to remove null value objects:

for ( int i = 0; i < myArrayList.Length; i++ )
{
if (myArrayList[i] == null )
myArrayList.RemoveAt[i]);
}

The ArrayList is in many ways extremely practical, as you don't have to redim the size. You just simply remove the unwanted elements.

0 comments:

Post a Comment