If I have an array and an array list:

int[] myArray = new int[2];
myArray[0] = 1;
myArray[1] = 2;

ArrayList myArrayList = new ArrayList();
myArrayList.Add("1st element");
myArrayList.AddRange(myArray);

By calling the Addrange() method in myArrayList, does this add the contents of myArray to the second element of myArrayList? If so how do you access the second element of myArray through myArrayList?

Recommended Answers

All 4 Replies

The AddRange function simply adds all of the objects in the added range to the arraylist. In your example, your arraylist would contain three items accessible at positions 0, 1 and 2.
Think of it in terms as appending the array to the arraylist, it just gets tacked on the end.

Ok so now would my arraylist have the contents of at the following indexes:
[0] = "1st element"
[1] = 1 (1st element in array
[2] = 2 (2nd element in array)?

Exactly. It works like the zip or merge functions available in other languages, suching iterating throught the items in the object to be added and pushing them onto the add of the arraylist.
Auto increasing of the size of the arraylist is taken care of too so you don't need to worry about extending the arraylist past it initialised size before calling addRange.

gotcha thankyou

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.