Is there a way to shrink an array to fit?

I'm curious if there's a way to shrink an array to fit.

For example I have an array that I will be adding text into and then removing it after I process it.  So when it's processed then I will remove that item from the array (starting from the bottom).  Therefore I may have an array that starts at 5 and ends at 20.

I'm curious to know if there's a way to shrink the array with all the data in it down to an array that starts at 0 and ends at 14.

I know it can be done by copying the array over line by line with a loop, but I wasn't sure if there was a better way to do this.
Who is Participating?
Bob HoffmanDeveloperCommented:
I would create a structure to hold your data and load the structure to a collection. See snippet.
Not sure how much data you're working with so as far as performance I can't really comment. You could also keep the array approach but make it an object array and load the structure to the array, this would eliminate the need for multiple arrays. To manage the size you could keep track of the number of items you have removed and when you hit some threshold reload the array, maybe 20% deleted records?

    Dim cCollection As New Collection
    'Structure needs to be in a Module
    Public Structure CollectionData
        Dim Field1 As String
        Dim Field2 As String
        Public Sub New(ByVal _Field1 As String, ByVal _Field2 As String)
            Field1 = _Field1
            Field2 = _Field2
        End Sub
        Public Overrides Function ToString() As String
            Return Me.Field1
        End Function
    End Structure
    Public Sub WorkWithCollection()
        Dim cData As New CollectionData
        Dim strKey As String = "Some unique key to access the item later"
        'Load the structure
        cData.Field1 = "Some info"
        cData.Field2 = "Some additional info"
        'Add to collection
        cCollection.Add(cData, strKey)
        'Get the data back
        cData = cCollection.Item(strKey)
        'Remove the item
    End Sub

Open in new window

Bob HoffmanDeveloperCommented:
Redim Preserve YourArray(14)
mystamiAuthor Commented:
That would provide a smaller array but would not make the information move down from 5 to now start at 0 with all the other information as well and the last one now being 14 instead of 20.
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Bob HoffmanDeveloperCommented:
Yea, if your pulling items out of the top or the middle the only way to shrink the array would be to reload it. Have you considered using a collection instead of an array?
mystamiAuthor Commented:
I haven't, actually, I forgot about collections!

Have you had any experience with the processing speed of Collections vs. Arrays?
mystamiAuthor Commented:
Is there any sort of multidimension collection because I need to be able to store 2 values, one value being the winsock user and the other is the message to send.
mystamiAuthor Commented:
Of course 2 collections can always be used but I need something that's going to run as fast as possible.
Bob HoffmanDeveloperCommented:
Did you get your question answered?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.