返回列表 回復 發帖

1442 monster beats by dr dre (26)

ch_client = "articlealley";
ch_width = 675;
ch_height = 200;
ch_sid = "Article Alley Articles North MPU";
ch_cid = "north";
ch_type = "mpu";
ch_hq = 1;
In previous post 'Convert Arraylist To Array' we discussed how to convert an ArrayList to an array. Today, let's see several examples about how to convert an array to an ArrayList (or a generic list). In some situation, we need to create  beats by dr dre discount an ArrayList based on an array, so we can easily add or remove items from the list later. Basically at least there are two ways to do it. The easiest  beats by dre studio monster one is to use the constructor of ArrayList to create a new instance from an Array directly. An overload version of the constructor accepts an ICollection parameter. And since Array implements ICollection interface, so could use below code to create an new instance of ArrayList:
string[] itemArray = new string[] { "item1", "item2", "item3" };
ArrayList arraylist = new ArrayList(itemArray);
Another method we could use to do the convertion is AddRange method. As same as the constructor it accepts an ICollection parameter. The usage is like
string[] itemArray = new string[] { "item1", "item2", "item3" };
ArrayList arraylist2 = new ArrayList(itemArray.Length); // set the initial size of the ArrayList to avoid possible performance issue.
arraylist2.AddRange(itemArray);
This is a bit complex than the 1st one, but it is a good choice when we already have an ArrayList. Now let's see the complete code example:
// The code is Created by Mavis
// and has been tested on vs2010 with .Net 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication11
{
        class Program
        {
                static void Main(string[] args)
                {
                        string[] itemArray = new string[] { "item1", "item2", "item3", "item4", "item5", "item6" };
                        ArrayList arraylist = new ArrayList(itemArray);
                        PrintIndexAndValues(arraylist);
                        // Copies the Queue elements to the end of the ArrayList.
                        ArrayList arraylist2 = new ArrayList(itemArray.Length);
                        arraylist2.AddRange(itemArray);
                        PrintIndexAndValues(arraylist);
                }
                public static void PrintIndexAndValues(ArrayList myList)
                {
                        Console.WriteLine("The Array List contains the following values:");
                        int i = 0;
                        foreach (Object o in myList)
                                Console.WriteLine("        [{0}]:        {1}", i++, o);
                        Console.WriteLine();
                }
        }
        /*
The Array List contains the following values:
[0]: item1
[1]: item2
[2]: item3
[3]: item4
[4]: item5
[5]: item6
The Array List contains the following values:
[0]: item1
[1]: item2
[2]: item3
[3]: item4
[4]: item5
[5]: item6
}
In the end of this post, I would like to talk some advanced topic about these two convertion ways. Do you know the different between them? If we use Red Gate's .NET Reflector to look at the inside of ArrayList constructor, we will see they are actually same. What constructor uses to initialize ArrayList is an exception check, size initialization and an AddRange method which is just as same as what  review beats by dre we use in the 2nd method. The constructor source code is like below.
ch_client = "articlealley";
ch_width = 675;
ch_height = 200;
ch_sid =  where to sell beats by dre "Article Alley Articles North MPU";
ch_cid = "north";
ch_type =  red sox monster beats "mpu";
ch_hq = 1;
// the code is decompilation from Microsoft.NETFrameworkv2.0.50727mscorlib.dll by Red Gate's .NET Reflector
public ArrayList(ICollection c)
{
if (c == null)
{
throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection"));
}
this._items = new object[c.Count]; // this line is to set the size of  beats studio for cheap ArrayList, it's quite same as we call new ArrayList(int)
this.AddRange(c);
}
Now it is the time to try the code by yourself and don't forget leave comments at my bolg.

Related articles:

  
   1108 monster beats by dr dre (35)
返回列表