CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2002
    Location
    Belgium
    Posts
    125

    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,

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Trim Array & Count

    Quote 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

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    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)

  4. #4
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    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)

  5. #5
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    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.

  6. #6
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Trim Array & Count

    In .Net 2.0 u have List Generic Class that u must prefer to use.

  7. #7
    Join Date
    Jan 2006
    Posts
    293

    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
  •  





Click Here to Expand Forum to Full Width

Featured