[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?
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?
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:
Dim existingArray As Array
Dim arrayToAdd As Array
'...
'Resize the existing array to add room for the elements of the array to add.
Array.Resize(existingArray, existingArray.Length + arrayToAdd.Length)
'Copy the elements of the array to add to the new elements of the existing array.
arrayToAdd.CopyTo(existingArray, existingArray.Length - arrayToAdd.Length)
If you did use a collection you could simply do this:
VB Code:
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).
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:
Dim arr_Results as array
Dim arr_ResultsStore as array
'This is where i add all the information from the XML file to arr_Results
'Resize array
arr_ResultsStore.Resize(arr_ResultsStore, arr_ResultsStore.Length + arr_Results.Length)
'Copy array elements
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?
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:
Dim arr_Results As String()
Dim arr_ResultsStore As String()
Dim originalLength As Integer = arr_ResultsStore.Length
'This is where i add all the information from the XML file to arr_Results
'Resize array
Array.Resize(arr_ResultsStore, originalLength + arr_Results.Length)
'Copy array elements
arr_Results.CopyTo(arr_ResultsStore, originalLength)
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