3

Can the second dimension be initialized as dynamically sizeable?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user225626
  • 1,091
  • 5
  • 16
  • 32
  • You mean different for each instance of the first dimension? Like an array of variable-length vectors? – n8wrl Jan 14 '11 at 20:43
  • Possible duplicate of this question: http://stackoverflow.com/questions/50558/how-do-you-initialize-a-2-dimensional-array-when-you-do-not-know-the-size – Dan J Jan 14 '11 at 20:43
  • @djacobson: Voting a dupe auto-posts a comment for you. – BoltClock Jan 14 '11 at 20:44
  • @BoltClock That's what I thought, but I didn't see it occur immediately, and posted my own. Duplicate duplicate comment removed. :) – Dan J Jan 14 '11 at 20:46

5 Answers5

6

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

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • Thank you. I think I meant to ask about a jagged array anyway. So how do I go about initializing a jagged array containing an ArrayList() or List<> as the second element in a jagged array? Thank you for any help. Please feel free to use the Answer area: – user225626 Jan 14 '11 at 20:57
4

You mean a jagged array? Like this:

class Program
{
  public int[][] jaggedArray = {
                                 new int[]{ 1 } ,
                                 new int[]{} ,
                                 new int[]{ 1 , 2 , 3 } ,
                               } ;
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1

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++;
        }
}
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
  • Looking to your pseudo code I'd say you'll need something like List[] = new List[924](); – Kees C. Bakker Jan 14 '11 at 21:44
  • Thanks. Because I ended up starting with the other one because it looked easier to write, me being a noob, I composed and compiled something in that and will test it (which may happen within minutes or hours). If that doesn't work, I'll come back to this fast. Thanks again for answering and your help. EDIT: Kees, because you may be around, I'll post anew what I just wrote in the other config. (not having tested it yet), and ask if you can edit my snip for how yours would be written in the same syntax please if you want. Thanks. – user225626 Jan 14 '11 at 21:50
  • PLEASE SEE MY LATEST EDIT TO POST ABOVE. – user225626 Jan 14 '11 at 22:16
  • You'll need to check if the List has been initialized. You'll start with values. If you're processing images, it might be better to keep using jagged arrays. Try to avoid using ArrayList, because a List stores the values in a more effective way. – Kees C. Bakker Jan 14 '11 at 22:35
  • I'm getting Error 'Method name expected' for the initialization line. new List[924] is squiggly underlined in blue. – user225626 Jan 14 '11 at 22:48
  • Thank you! for helping me. Very much appreciated. Still can't figure out how to get around the 'Method name expected' thing. (If I don't hear from you again, THANKS AGAIN; because of your generous help, I'm light years ahead of where I had been.) – user225626 Jan 14 '11 at 22:59
  • Sorry for the late reaction. Please give me the whole line of code that give you the error :-) – Kees C. Bakker Jan 18 '11 at 23:02
0

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];
Unnati
  • 2,441
  • 17
  • 37
0

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++;
        }
    }
user225626
  • 1,091
  • 5
  • 16
  • 32
  • Hm... what are you trying to accomplish? I mean functionally... are you trying to cut out a piece of the bitmap or something? – Kees C. Bakker Jan 14 '11 at 22:16
  • You know what a candlestick chart looks like? Imagine the same look as an outline that's been left by the movement of a snip like this. (Except that I'm using the code to screen scrape the path of a line on a line chart, including the antialiasing.) – user225626 Jan 14 '11 at 22:21
  • "...are you trying to cut out a piece of the bitmap or something?" Yes. – user225626 Jan 14 '11 at 22:22
  • ow... your code doesn't work, because you're missing a reset of the nV variable. I think when you do nH++, you probably should also do a nV = 0. – Kees C. Bakker Jan 14 '11 at 22:31
  • nV = 0 was a good catch, thanks. (As for the other line, sVertRange[nH].Add(v); won't compile.) I don't know what to do. If you think your List thing might work, I don't know how to compose that at all. – user225626 Jan 14 '11 at 22:34
  • I'm following your thread upstairs. I really want to thank you for writing that. I'm still getting the 'Method name expected' error for the initialization line, but, really, thank you for that snip. I never would have thought of how that's put together. – user225626 Jan 14 '11 at 22:55
  • This ended up working. I wasn't resetting the necessary array elements to zero and back to zero. Thanks again. – user225626 Jan 15 '11 at 00:25