Braintique.com header
Left Navigation Bar

Array of Arrays

An array of arrays, also called a jagged array because of its “unevenness” compared to a standard n-dimensional array, is an array where each row is a one-dimensional array. Jagged arrays can be declared and instantiated in a single statement—using side-by-side square braces—but there is no way to initialize the elements of the jagged array in the same statement.

For example, here’s how you might declare a two-dimensional jagged string array, with the first dimension having seven rows and the second dimension varying—considerably—in the number of elements:

const int rows = 7;
string [] [] jaggedA = new string [rows] [];


jaggedA[0] = new string [2];
jaggedA[1] = new string [3];
jaggedA[2] = new string [1];
jaggedA[3] = new string [4];
jaggedA[4] = new string [40];
jaggedA[5] = new string [2];
jaggedA[6] = new string [86];

Next, individual elements could be assigned values. For example:

jaggedA [1] [2] = "jagged";

Some of the jagged one-dimensional arrays within arrays are filled using iteration:

for (int dash = 0; dash < 86; dash++)
jaggedA [6] [dash] = "-";

Next, each “column” can be displayed as a single concatenated item in a ListBox:

string column = "";
for (int i = 0; i < 2; i++) {
column = column + " " + jaggedA [0] [i] + " ";
}
lstMulti.Items.Add(column);

The listing below shows the rather messy code that does this for entire seven rows (messy because with each “column” array a different size, nested iteration is not easily possible). If you run the code, a graph depiction of “jaggedness” will appear in the ListBox.

ListBox

Listing: Declaring, Populating and Displaying a Jagged String Array

private void btnJagged_Click(object sender, System.EventArgs e) {
// declare a jagged array with 7 rows
const int rows = 7;
string [] [] jaggedA = new string [rows] [];


// give it some column arrays
jaggedA[0] = new string [2];
jaggedA[1] = new string [3];
jaggedA[2] = new string [1];
jaggedA[3] = new string [4];
jaggedA[4] = new string [40];
jaggedA[5] = new string [2];
jaggedA[6] = new string [86];


// populate it
jaggedA [0] [0] = "This";
jaggedA [0] [1] = "is";


jaggedA [1] [0] = "a";
jaggedA [1] [1] = "very";
jaggedA [1] [2] = "jagged";


jaggedA [2] [0] = "array.";


jaggedA [3] [0] = "It";
jaggedA [3] [1] = "looks";
jaggedA [3] [2] = "extremely";
jaggedA [3] [3] = "uneven.";


// fill the final three columns with dashes (-)


for (int dash = 0; dash < 40; dash++)
jaggedA [4] [dash] = "-";


for (int dash = 0; dash < 2; dash++)
jaggedA [5] [dash] = "-";


for (int dash = 0; dash < 86; dash++)
jaggedA [6] [dash] = "-";


// display it
string column = "";
for (int i = 0; i < 2; i++){
column = column + " " + jaggedA [0] [i] + " ";
}
lstMulti.Items.Add(column);


column = "";
for (int i = 0; i < 3; i++){
column = column + " " + jaggedA [1] [i] + " ";
}
lstMulti.Items.Add(column);


column = "";
for (int i = 0; i < 1; i++){
column = column + " " + jaggedA [2] [i] + " ";
}
lstMulti.Items.Add(column);


column = "";
for (int i = 0; i < 4; i++){
column = column + " " + jaggedA [3] [i] + " ";
}
lstMulti.Items.Add(column);


column = "";
for (int i = 0; i < 40; i++){
column = column + jaggedA [4] [i];
}
lstMulti.Items.Add(column);


column = "";
for (int i = 0; i < 2; i++){
column = column + jaggedA [5] [i];
}
lstMulti.Items.Add(column);


column = "";
for (int i = 0; i < 86; i++){
column = column + jaggedA [6] [i];
}
lstMulti.Items.Add(column);
}

It’s time to move on! The next part of this crash course explains why string manipulation is important and how string manipulation works in C#

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write