3

Can the second dimension be initialized as dynamically sizeable?

4
  • You mean different for each instance of the first dimension? Like an array of variable-length vectors? Commented Jan 14, 2011 at 20:43
  • Possible duplicate of this question: stackoverflow.com/questions/50558/… Commented Jan 14, 2011 at 20:43
  • @djacobson: Voting a dupe auto-posts a comment for you. Commented Jan 14, 2011 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. :) Commented Jan 14, 2011 at 20:46

5 Answers 5

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

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:
4

You mean a jagged array? Like this:

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

Comments

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++;
        }
}

7 Comments

Looking to your pseudo code I'd say you'll need something like List<int>[] = new List<int>[924]();
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.
PLEASE SEE MY LATEST EDIT TO POST ABOVE.
You'll need to check if the List<int> has been initialized. You'll start with <null> values. If you're processing images, it might be better to keep using jagged arrays. Try to avoid using ArrayList, because a List<int> stores the values in a more effective way.
I'm getting Error 'Method name expected' for the initialization line. new List<int>[924] is squiggly underlined in blue.
|
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++;
        }
    }

7 Comments

Hm... what are you trying to accomplish? I mean functionally... are you trying to cut out a piece of the bitmap or something?
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.)
"...are you trying to cut out a piece of the bitmap or something?" Yes.
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.
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.
|
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];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.