Solved

Is there a way to shrink an array to fit?

Posted on 2009-05-14
9
344 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

785 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