#127 – Declaring and Instantiating Jagged Arrays

It’s possible in C# to declare and instantiate jagged arrays. A jagged array is one that can have a different number of columns in each row (assuming the array is two-dimensional).  A jagged array can also be considered to be an array of arrays.

When you declare and instantiate a jagged array, you specify the size of only the first dimension.

// Jagged array - 3 elements, each of which is array of int
 int [][] nums2 = new int[3][];

After initialization, the jagged array consists of a one-dimensional array of arrays, with each element of the array initialized to null.

As a separate step, you can then initialize each element of the jagged array as a separate array, each of which can be a different size.

 nums2[0] = new int[4];
 nums2[1] = new int[2];
 nums2[2] = new int[10];
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #127 – Declaring and Instantiating Jagged Arrays

  1. Pingback: #1,016 – Retrieving the Length of an Array | 2,000 Things You Should Know About C#

  2. Pingback: #1,124 – Iterate through Jagged Array with Nested foreach | 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: