ASP.NET Basics (Part 5): Cooking Up a Storm - Jagged Little Pill
(Page 8 of 8 )
In all the examples on the previous pages, the arrays have been symmetrical, in that every row of the array has had an equal number of elements. For example, an array defined as
meals
[3,3]
will always hold 9 values, with each row holding an equal number of columns (3). Of course, it is possible that some of these 9 elements might not be assigned any value; in this case, they will hold the default value defined by C# for the data type of that array.
C# also allows you to define an array that does not have the same number of columns across its rows. It's called a "jagged array", and here's an example:
<%
string [][] meals= new string [3][];
meals [0] = new string[1];
meals [1] = new string[3];
meals [2] = new string[2];
meals[0][0] = "cream of mushroom soup";
meals[1][0] = "pasta";
meals[1][1] = "lasagne";
meals[1][2] = "tandoori chicken";
meals[2][0] = "chocolate mousse";
meals[2][1] = "tiramisu";
%>
Since a jagged array itself contains other arrays, it is not surprisingly often referred to as an "array of arrays".
How about an example? Here's a script that defines a jagged array, assigns values to its elements and displays the values using a "for" loop:
<SCRIPT language=c# runat="server">
void Page_Load()
{
// store the names of the courses
string [] courses = {"Starters"," Main Course Dishes", "Desserts"};
// define the array
string [][] meals= new string [3][];
meals [0] = new string[1];
meals [1] = new string[3];
meals [2] = new string[2];
// assign values to the elements
// for the first row, starters
// one column
meals[0][0] = "cream of mushroom soup";
// for the second row, main courses
// three columns
meals[1][0] = "pasta";
meals[1][1] = "lasagne";
meals[1][2] = "tandoori chicken";
// for the third row, desserts
// two columns
meals[2][0] = "chocolate mousse";
meals[2][1] = "tiramisu";
// a "for" loop to iterate through the array
for(int i = 0; i < meals.Length; i++)
{
output.Text = output.Text + "<tr>";
output.Text = output.Text + "<td><b>" + courses[i] + "</b></td>";
for(int j = 0; j < meals[i].Length; j++)
{
output.Text = output.Text + "<td>" + meals[i][j] + "</td>";
}
output.Text = output.Text + "</tr>";
}
}
</SCRIPT>
<TABLE cellSpacing=0 cellPadding=5 width="75%" border=0><asp:label id=output runat="server"></asp:label>
<TBODY></TBODY></TABLE>
Here's the output.
The script starts with the definition of the jagged array.
<%
// define the array
string [][] meals= new string [3][];
meals [0] = new string[1];
meals [1] = new string[3];
meals [2] = new string[2];
// assign values to the elements
// for the first row, starters
// one column
meals[0][0] = "cream of mushroom soup";
// for the second row, main courses
// three columns
meals[1][0] = "pasta";
meals[1][1] = "lasagne";
meals[1][2] = "tandoori chicken";
// for the third row, desserts
// two columns
meals[2][0] = "chocolate mousse";
meals[2][1] = "tiramisu";
// snip
%>
Note the manner in which I have assigned values to the various elements. A comparison with previous examples will illustrate the difference between this syntax, and that used for "regular" multi-dimensional arrays.
<%
// snip
// the "for" loop to iterate through the array
for(int i = 0; i < meals.Length; i++)
{
output.Text = output.Text + "<tr>";
output.Text = output.Text + "<td><b>" + courses[i] + "</b></td>";
for(int j = 0; j < meals[i].Length; j++)
{
output.Text = output.Text + "<td>" + meals[i][j] + "</td>";
}
output.Text = output.Text + "</tr>";
}
// snip
%>
Once again, I have used two "for" loops to iterate over the array. Note my usage of the "Length" property to find the number of elements at each level; this ensures that the script does not attempt to access an element that does not exist.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |