开发者

2 dimensional array in C#

Can the second dimension be initialized as dynami开发者_JS百科cally sizeable?


No (since C# array dimensions are fixed), but you could create an array of List<T>.


You mean a jagged array? Like this:

class Program
{
  public int[][] jaggedArray = {
                                 new int[]{ 1 } ,
                                 new int[]{} ,
                                 new int[]{ 1 , 2 , 3 } ,
                               } ;
}


Normally a jagged array has a size. You could use two collections:

List<List<int>> list = new List<List<int>>();

You can access the values the same way you would access any array. The advantage is that you don't need to specify the size at creation.

Edit: if the "outer" array is fixed, you could use:

List<int>[] list = new List<int>[100]();

Edit: looking to your example I'd say something like this could do the trick:

List<int>[] sVertRange = new List<int>[924];

int nH = 0;
for (int h = 315; h < 1240; h++) 
{
    for (int v = 211; v <= 660; v++) 
    {
            Color c = bmp.GetPixel(h, v);
            if (c.R > 220 && c.G < 153) 
        {
            if(sVertRange[nH] == null)
            {
                sVertRange[nH] = new List<int>();
            }

                    sVertRange[nH].Add(v);
            }
            nH++;
        }
}


UPDATE: I just tested this and it doesn't work--it crashes immediately. So how is the following written in Kees's syntax?

    int[][] sVertRange = {new int[924] ,new int[]{}};  
    int nH = 0;
    int nV = 0;
    for (int h = 315; h < 1240; h++) {
        for (int v = 211; v <= 660; v++) {
            Color c = bmp.GetPixel(h, v);
            if (c.R > 220 && c.G < 153) {
                sVertRange[nH][nV++] = v;
            }
        nH++;
        }
    }


if you want to create 2 dimensional array in C# each element type is bitmap

int num1 = 10,num2 = 20;
Bitmap[][] matrix= new Bitmap[num1][]; 
for (int i = 0; i < num1; i++ )
     matrix[i] = new Bitmap[num2];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜