Results 1 to 6 of 6

Thread: [RESOLVED] [2005] what would be better....array or arrayList

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Resolved [RESOLVED] [2005] what would be better....array or arrayList

    Hi guys,
    I have a little problem which i was hoping someone could advise me on. I download an XML file and go through it to get all the information from it. The XML file has the following layout
    <results>
    <result>
    <time> </time>
    <name> </name>
    <method> </method>
    </result>
    </results>
    I store the information from the 3 nodes in an array. This is all done on a separate thread which runs off a timer with an interval of 10000. So everytime a new XML file is downloaded the previous elements in the array are overwritten. Now i need to set up another array to use the information from the XML in another way. Now everytime i try to store the information from the XML file in the separate array i need it to add it to the end of the array so that all the previous results aren't lost. Is there a way i could do this or would i be better off storing the results in an arrayList and just using the .Add function for the list?

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] what would be better....array or arrayList

    I know this might sound a bit confusing so here's the breakdown i what part of my program does:

    timer ticks
    downloads XML file
    extract results
    store in array (eg arr_Results)
    add arr_Results to end of separate array (eg arr_ResultsStored)

    i just cant get it to add to the end of the separate array so i was wondering whether using an arrayList would be better?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] what would be better....array or arrayList

    You can't add to an array. You have to create a new array and then copy the elements of the other two arrays into it. Some of that complexity can be hidden like this:
    VB Code:
    1. Dim existingArray As Array
    2. Dim arrayToAdd As Array
    3.  
    4. '...
    5.  
    6. 'Resize the existing array to add room for the elements of the array to add.
    7. Array.Resize(existingArray, existingArray.Length + arrayToAdd.Length)
    8.  
    9. 'Copy the elements of the array to add to the new elements of the existing array.
    10. arrayToAdd.CopyTo(existingArray, existingArray.Length - arrayToAdd.Length)
    If you did use a collection you could simply do this:
    VB Code:
    1. myCollection.AddRange(myArray)
    Note that ArrayLists should very rarely be used in VB 2005. ArrayList items are Object references, which means absolutely any type of object can be added to the collection and you have to cast the items when you retrieve them. Only use an ArrayList if you specifically want to be able to add unrelated types to the same collection. Otherwise you should use a generic List. For instance, if you want to store String objects in your collection you would declare a List(Of String).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] what would be better....array or arrayList

    Thanx for the quick reply jmcilhinney...
    I just need to clear something up, say for example that the array i was initially storing all the information from the XML file to was called arr_Results and the new array i want to appear as though i'm adding to is called arr_ResultsStore....would the code look as follows
    VB Code:
    1. Dim arr_Results as array
    2. Dim arr_ResultsStore as array
    3.  
    4. 'This is where i add all the information from the XML file to arr_Results
    5. 'Resize array
    6. arr_ResultsStore.Resize(arr_ResultsStore, arr_ResultsStore.Length + arr_Results.Length)
    7.  
    8. 'Copy array elements
    9. arr_Results.CopyTo(arr_ResultsStore, arr_ResultsStore.Length - arr_results.Length)

    Have i got this right or have i got my arrays the wrong way round? Also when copying the array element would it copy them to the beginning or the end of arr_ResultsStore?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] what would be better....array or arrayList

    That's correct except for one thing. Resize is a Shared method, so you call it on the Array class itself, as I did in the code I posted, rather than an array instance. All you would need to do is substitute "arr_ResultsStore" for "existingArray" and "arr_Results" for "arrayToAdd". Also, you wouldn't declare them as type Array. I did that for generality but you would declare them as the type of the objects they were to contain, e.g. String() rather than Array. Also, you could make that just a litle more efficient by not performing the redundant calculation:
    VB Code:
    1. Dim arr_Results As String()
    2. Dim arr_ResultsStore As String()
    3.  
    4. Dim originalLength As Integer = arr_ResultsStore.Length
    5.  
    6. 'This is where i add all the information from the XML file to arr_Results
    7. 'Resize array
    8. Array.Resize(arr_ResultsStore, originalLength + arr_Results.Length)
    9.  
    10. 'Copy array elements
    11. arr_Results.CopyTo(arr_ResultsStore, originalLength)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] what would be better....array or arrayList

    Ok thanx, i've never used Array.Resize before so that probably why i changed in....didn't realise i had...also i wasn't gonna declare the arrays as array i have them declared as structures....but thanx again for all your help

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