Solved

Is there a way to shrink an array to fit?

Posted on 2009-05-14
9
343 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:mystami
  • 4
  • 4
9 Comments
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24390904
Redim Preserve YourArray(14)
       
0
 
LVL 1

Author Comment

by:mystami
ID: 24391103
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.
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24391151
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?
0
 
LVL 1

Author Comment

by:mystami
ID: 24391714
I haven't, actually, I forgot about collections!

Have you had any experience with the processing speed of Collections vs. Arrays?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:mystami
ID: 24391759
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.
0
 
LVL 1

Author Comment

by:mystami
ID: 24391779
Of course 2 collections can always be used but I need something that's going to run as fast as possible.
0
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 125 total points
ID: 24397618
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

        cCollection.Remove(strKey)

    End Sub

Open in new window

0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24540066
Did you get your question answered?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now