3

I have the below code:

    public int[] _SpDep = new int[50];
    public int[][] _SpDepCnt = new int[50][];
    public int[][] _SpReadType = new int[50][];

     _DepNo = Convert.ToInt16(strFileName[n].Substring(1, 2));
     _CntNo = Convert.ToInt16(strFileName[n].Substring(6, 2));
     _SpDep[_DepNo] = 1;
     _SpDepCnt[_DepNo][_CntNo] = 1;
     _SpReadType[_DepNo][_CntNo] = 1;

There's an error when reach this line:

      _SpDepCnt[_DepNo][_CntNo] = 1;

But I don't know what's wrong? Any opinions? Is it the 2d array declare wrongly?

2
  • U mean when I put [50] means the value must be between 0 to 49? I tot I got 50 array values. For example: _DepNo[0], _DepNo[1]....._DepNo[49]. Commented Apr 3, 2013 at 8:11
  • @Coolguy: Correct, array indicies start at 0, so for 50 values they go from 0..49 Commented Apr 3, 2013 at 8:19

5 Answers 5

5

Jagged array [][]

If using an array of type int[][] (a jagged array) you want to be initialise your arrays like this:

public int[] _SpDep = new int[50];
public int[][] _SpDepCnt = new int[50][];
public int[][] _SpReadType = new int[50][];

And then initialise the arrays inside the array:

var length = 20;
for (int i = 0; i < length; i++)
{
    _SpDepCnt[i] = new int[length];
     _SpReadType[i] = new int[length];
}

It is called a jagged array because the lengths of the second part can vary, you could have this for example:

[1,2,3,4]
[5,6]
[7,8,9]

Multidimensional array [,]

I believe you want to use the type int[,] which is called a multidimensional array. They make an array with two fixed dimensions.

public int[,] _SpDepCnt = new int[50, 20];
public int[][] _SpReadType = new int[50, 20];

Multidimensional arrays will create the same sized array for each index:

[1,2,3]
[4,5,6]
[7,8,9]
Sign up to request clarification or add additional context in comments.

Comments

3

When you declare an "array of arrays" using [][], you need to initialise the outer dimension to reference allocated arrays, like this:

public int[][] _SpReadType = new int[50][];

for (int i = 0; i < 50; ++i)
    _SpReadType[i] = new int[SIZE]; // Where you have to decide on SIZE

The [][] arrays are also known as "ragged arrays" or "jagged arrays" because the size of each row can be different, since each row is a separate array.

Alternatively, you can use actual 2D arrays using this syntax: [,]

For example:

public int[,] _SpReadType = new int[50,SIZE];

Then you access the elements like so:

int value = _SpReadType[row,col];

2 Comments

I just want an 2d array like this _DepNo[1][1]=1; _DepNo[1][2]=1; Something like that...
I recommend you use the 2D array syntax, _DepNo[1,1]=1; and declare it as int[,] _DepNo = new int[ROWS,COLS];
1

that's a jagged array you have to initialize the element before you can use it

myJaggedArray[0] = new int[5];
myJaggedArray[1] = new int[4];
myJaggedArray[2] = new int[2];

http://msdn.microsoft.com/en-us/library/2s05feca(v=vs.71).aspx

Comments

1

What you tried is called jagged arrays, not multidimensional arrays.

If you want to use 2 dimennsional array, you can use it like;

 public int[,] _SpDepCnt = new int[50, 50];
 _SpDepCnt[_DepNo, _CntNo] = 1;

If you want to use jagged array, you can use it like;

public int[][] _SpReadType = new int[50][];

for (int i = 0; i < 50; i++)
    _SpReadType[i] = new int[SIZE]; 

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."

1 Comment

perhaps this should have been a comment
0

You must reserve memory for the 2D-Array

public int[][] _SpDepCnt = new int[50][];
_SpDepCnt[_DepNo] = new int[50];
_SpDepCnt[_DepNo][_CntNo] = 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.