-
October 27th, 2006, 11:08 AM
#1
Trim Array & Count
Hello,
I would like to cut off my array so it only has the 10 first / last items.
Should I create a for x = 1 to 10 .. and copy them in another array, or is there a function?
Also, I would like to count te number of times a certain string / int is in the Array.
Much thanks in forward!
Bert Willekens,
-
October 27th, 2006, 11:16 AM
#2
Re: Trim Array & Count
Originally Posted by Bert Willekens
I would like to cut off my array so it only has the 10 first / last items.
Should I create a for x = 1 to 10 .. and copy them in another array, or is there a function?
That is one option. Another one is to use an ArrayList instead of an array. ArrayList has different functions for inserting, removing and rearranging data. You can also convert an array to (and from) an ArrayList.
- petter
-
October 27th, 2006, 11:32 AM
#3
Re: Trim Array & Count
cut off my array so it only has the 10 first / last items
Code:
Dim arr As New ArrayList
'Add items
For i As Integer = 0 To 19
arr.Add("a" & i)
Next
'Display items
Dim st As String = ""
For i As Integer = 0 To arr.Count - 1
st += arr.Item(i) & " : "
Next
MessageBox.Show(st)
' Remove first and last 5
For i As Integer = 0 To 4
arr.RemoveAt(arr.Count - 1)
arr.RemoveAt(0)
Next
'Display items
st = ""
For i As Integer = 0 To arr.Count - 1
st += arr.Item(i) & " : "
Next
MessageBox.Show(st)
-
October 27th, 2006, 11:39 AM
#4
Re: Trim Array & Count
Code:
'Declare and initialize array
Dim str(19) As String
For i As Integer = 0 To 19
str(i) = "a" & i
Next
'Show array Items
Dim st As String = ""
For i As Integer = 0 To str.Length - 1
st += str(i) & " : "
Next
MessageBox.Show(st)
'Remove First 5 items
Array.Reverse(str)
Array.Resize(str, str.Length - 5)
'Remove last 5 items
Array.Reverse(str)
Array.Resize(str, str.Length - 5)
'Show array Items
st = ""
For i As Integer = 0 To str.Length - 1
st += str(i) & " : "
Next
MessageBox.Show(st)
-
October 27th, 2006, 11:53 AM
#5
Re: Trim Array & Count
Also, I would like to count te number of times a certain string / int is in the Array.
using Array.findAll to get the array with matched strings (i-e if str.item="a")
Code:
'Declare and initialize array
Dim str(19) As String
For i As Integer = 0 To 19
str(i) = "a"
Next
'Find the occurances of string "a"
MessageBox.Show(Array.FindAll(str, AddressOf MyPredicate).Length)
Code:
Function MyPredicate(ByVal s As String) As Boolean
If s.Equals("a", StringComparison.InvariantCultureIgnoreCase) Then
Return True
Else
Return False
End If
End Function
Last edited by aniskhan; October 27th, 2006 at 11:57 AM.
-
October 27th, 2006, 12:01 PM
#6
Re: Trim Array & Count
In .Net 2.0 u have List Generic Class that u must prefer to use.
-
October 27th, 2006, 10:36 PM
#7
Re: Trim Array & Count
Aniskhan is correct. If using 2005, then Generic Lists should be used over Arraylists if you are using objects all of one particular type. There is a pretty significant performance gain using Generic Lists compared to Arraylists, especially when you start getting into larger and larger collections, since Generic Lists are optimized for whatever type you declare it as. The only reason I can see to still use ArrayLists in 2005 is if you are wanting to store an assortment of objects of different types in one collection. Otherwise, use Generic Lists.
Last edited by gigemboy; October 29th, 2006 at 11:53 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|