Monday, May 5, 2008

Add array to arraylist

I'd like to elaborate a bit more on arraylists. What if you utilize a class that returns an array. Maybe you made a call to a database and it returns you an array. But you want to use the nice stuff..eh. like in the arraylist..!

Well, we can still use the arraylist. In order to use the arraylist we have to add our array to an arraylist. We will use the addRange method. Here is how:

We will use our good old cars array.

dim cars(2) as string
dim listcars as new ArrayList

cars(0) = "ford"
cars(1)= "chevy"

listcars.AddRange(cars)

There you go, we added our cars array to our arrayList listcars. So now we are able to use all the nice methods that listcars exposes.

I don't know yet why Microsoft added the array and arraylist, why not use the arraylist?

happy programming

No comments: