How to determine size/length of array

Hogfarmer

Member
Joined
Jul 29, 2009
Messages
8
Programming Experience
1-3
I want to determine size/length of an array that is, how many elements are in my 1 dimen array, can't seem to find the right method that works.
 
Length. You should be able to find tutorials on arrays just about any place you look.
 
Length property is one option. Collections have a Count property.
VB.NET:
Dim elements As Integer = New String() {"a", "b"}.Length
 
ok, I got that, if I may, the other issue concerning arrays was how to redimension /resize an array so that all values of zero get zapped out. So if my array was the following:
a(1)=66
a(2)=55
a(3)=0
a(4)=77
a(5)=0

and I wanted to end up with:
a(1)=66
a(2)=55
a(3)=77
..end of array.....
 
You should look into collections/lists for this, they are used as dynamic arrays.
List(T) Class (System.Collections.Generic)
VB.NET:
Dim l As New List(Of Integer)(New Integer() {1, 2, 0, 3, 0, 4})
l.RemoveAll(Function(x) x = 0)
This collection of Integers would end up containing the elements {1,2,3,4}.
 
I agree with using the List(Of T) but he's marked as using .Net 2.0 and won't be able to use the lambda. Using a Predicate delegate will accomplish the same thing.

VB.NET:
		Dim l As New List(Of Integer)(New Integer() {1, 2, 0, 3, 0, 4})
		l.RemoveAll(AddressOf RemoveZeros)

VB.NET:
	Private Function RemoveZeros(ByVal x As Integer) As Boolean
		If x = 0 Then
			Return True
		Else
			Return False
		End If
	End Function

If he absolutely has to have an array I'd still recommend temporarily using a list.

VB.NET:
		Dim a() As Integer = {1, 2, 0, 3, 0, 4}
		Dim tempList As New List(Of Integer)(a)
		tempList.RemoveAll(AddressOf RemoveZeros)
		a = tempList.ToArray
 
Thing is, I have this array already established and have done some operations on it like sorting and using ubound on it, I think re defining as a list would cause it to break?
 
You can call myList.Sort just like you can call Array.Sort(a).

If you need the upper bound you can use UBound(myList.ToArray) or myList.Count - 1.
 
MattP said:
he's marked as using .Net 2.0 and won't be able to use the lambda.
Correct, I didn't notice that.
VB.NET:
	Private Function RemoveZeros(ByVal x As Integer) As Boolean
		If x = 0 Then
			Return True
		Else
			Return False
		End If
	End Function
This function should be simplified to this:
VB.NET:
	Private Function RemoveZeros(ByVal x As Integer) As Boolean
		Return x = 0
	End Function
The expression "x = 0" evaluates to True or False.
Thing is, I have this array already established and have done some operations on it like sorting and using ubound on it, I think re defining as a list would cause it to break?
No, you can add the array to the list as shown with the constructor, or using the AddRange method, and continue working from there. You should also revaluate if using array in the first place is necessary, usually collections is the way to go, it's just a simplification of common array operations and includes memory optimizations. For example thelist.Sort does the same as Array.Sort(thearray).
 
How do I populate this list of integers? My regular array I was using before, I populated it via a loop such as:

For t = 1 To Len(Mad)
zstr = zstr + Mid(Mad, t, 1)
picks(t) = zstr
next

*******think I found it
picks.Add(zstr)
 
Last edited:
Back
Top