|
Jagged Arrays |
|
|
A jagged array is an array of arrays, or an array of
arrays of arrays, etc. To create a jagged array, use a combination of square
brackets for each dimension. The formula used is:
DataType[][] VariableName;
|
Each of the square brackets is used in any of the ways
we have introduced arrays so far. This means that the first square bracket
can be used as its own one-dimensional array or as a multi-dimensional
array. Here is an example:
string[2][5] members;
This declares a variable that represents two arrays and
each array internally contains 5 arrays. Because each pair of square
brackets is its own array, it can be used to create its own array with its
own multidimensional array. Here is an example that creates a
multidimensional array in the first dimension:
string[2,4][5] members;
In the same way, the second square bracket can be used
as a single or a multidimensional array. Here is an example:
string[2,4][5,12,8] members;
Initialization of a Jagged Array
|
|
When declaring a jagged array, you can allocate memory
for it using the new operator followed by the data type of the array
and the same combination of square brackets used to the left of the
assignment operator. The first pair of square brackets on the right side of
the assignment operator must contain the external dimension of the array.
The second pair of square brackets must be left empty. Here is an example:
using System;
public class Exercise
{
static int Main(string[] args)
{
string[][] members = new string[2][];
return 0;
}
}
To initialize a jagged array, when declaring the
variable, on the right side of the second pair of square brackets, provide
an opening and a closing curly brackets, then create each list in its own
pair of curly brackets. At the beginning of each list, you must allocate
memory for the list with the new operator. Here is an example:
using System;
public class Exercise
{
static int Main(string[] args)
{
string[][] members = new string[2][]{
new string[]{"Celeste", "Mathurin", "Alex", "Germain"},
new string[]{"Jeremy", "Mathew", "Anselme", "Frederique"} };
return 0;
}
}
If you initialize the array this way, you can omit
specifying the dimension of the external array. With a jagged array, you can
also initialize its internal array individually. To do this, access each
internal array by its zero-based index. Here is an example:
public class Exercise
{
static int Main(string[] args)
{
string[][] members = new string[2][];
members[0] = new string[]{"Celeste", "Mathurin", "Alex", "Germain"};
members[1] = new string[]{"Jeremy", "Mathew", "Anselme", "Frederique"};
return 0;
}
}
Application: Initializing a Jagged Array
|
|
- To declare and initialize jagged arrays, change the file as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DepartmentStore3
{
public class DepartmentStore
{
static int Main(string[] args)
{
long ItemID = 0;
string Description = "Unknown";
double Price = 0.00D;
// Each of the following variable
// arrays is structured as [2][2][4].
// Each variable represents:
// A/ Two major lists: The first major
// list represents women items,
// the second major list represents men items,
// B/ Two minor lists.
// Each of the major lists contains two minor lists:
// a/ The first minor list of the first major
// list contains adult women items
// The second minor list of the first major
// list contains girls items
// b/ The first minor list of the second major
// list contains adult men items
// The second minor list of the
// second major list contains boys items
// C/ Each minor list contains four items
long[][][] ItemNumber = new long[][][]
{
new long[][]
{
new long[]{947783, 934687, 973947, 987598, 974937},
new long[]{743765, 747635, 765473, 754026, 730302}
},
new long[][]
{
new long[]{209579, 267583, 248937, 276057, 267945},
new long[]{ 409579, 467583, 448937, 476057, 467945}
}
};
string[][][] ItemName = new string[][][]
{
new string[][]
{
new string[]
{
"Double-faced wool coat",
"Floral Silk Tank Blouse",
"Push Up Bra",
"Chiffon Blouse",
"Bow Belt Skirtsuit"
},
new string[]
{
"Cable-knit Sweater",
"Jeans with Heart Belt",
"Fashionable mini skirt",
"Double Dry Pants",
"Romantic Flower Dress"
}
},
new string[][]
{
new string[]
{
"Cotton Polo Shirt",
"Pure Wool Cap",
"Striped Cotton Shirt",
"Two-Toned Ribbed Crewneck",
"Chestnut Italian Shoes"
},
new string[]
{
"Under Collar and Placket Jacket",
"Country Coat Rugged Wear",
"Carpenter Jeans",
"Double-Cushion Tennis Shoes",
"Stitched Center-Bar Belt"
}
}
};
double[][][] UnitPrice = new double[2][][]
{
new double[][]
{
new double[]
{ 275.25, 180.00, 50.00, 265.00, 245.55 },
new double[]
{ 45.55, 25.65, 34.55, 28.55, 24.95 }
},
new double[][]
{
new double[]
{ 45.75, 25.00, 65.55, 9.75, 165.75 },
new double[]
{ 265.15, 35.55, 24.95, 48.75, 32.50 }
}
};
Console.Title = "Fun Department Store";
Console.Clear();
Console.WriteLine("==============================");
Console.WriteLine("Fun Department Store");
Console.WriteLine("==============================");
Console.WriteLine("Receipt");
Console.WriteLine("------------------------------");
Console.WriteLine("Item Number: {0}", ItemID);
Console.WriteLine("Description: {0}", Description);
Console.WriteLine("Unit Price: {0:C}", Price);
Console.WriteLine("==============================");
Console.ReadKey();
return 0;
}
}
}
Access to Members of a Jagged Array
|
|
As done for a multidimensional array, each member of a
jagged array can be accessed with a multiple index, depending on how the
array was created. Both the external and the internal lists are zero-based.
Here is an example:
using System;
public class Exercise
{
static int Main(string[] args)
{
string[][] members = new string[2][];
members[0] = new string[]{"Celeste", "Mathurin", "Alex", "Germain"};
members[1] = new string[]{"Jeremy", "Mathew", "Anselme", "Frederique"};
Console.WriteLine("Member 1: {0}", members[0][0]);
Console.WriteLine("Member 2: {0}", members[0][1]);
Console.WriteLine("Member 3: {0}", members[0][2]);
Console.WriteLine("Member 4: {0}", members[0][3]);
Console.WriteLine("Member 5: {0}", members[0][0]);
Console.WriteLine("Member 6: {0}", members[1][1]);
Console.WriteLine("Member 7: {0}", members[1][2]);
Console.WriteLine("Member 8: {0}\n", members[1][3]);
return 0;
}
}
This would produce:
Member 1: Celeste
Member 2: Mathurin
Member 3: Alex
Member 4: Germain
Member 5: Celeste
Member 6: Mathew
Member 7: Anselme
Member 8: Frederique
Press any key to continue . . .
You can also use some loops to access each member of the
array. Here is an example:
using System;
public static class Exercise
{
static int Main(string[] args)
{
string[][] members = new string[2][];
members[0] = new string[] { "Celeste", "Mathurin", "Alex", "Germain" };
members[1] = new string[] { "Jeremy", "Mathew", "Anselme", "Frederique" };
for (int External = 0; External < 2; External++)
for (int Internal = 0; Internal < 4; Internal++)
Console.WriteLine("Name: {0}", members[External][Internal]);
return 0;
}
}
If you want to use a foreach operator, you must
access each array by its index. The external array can be accessed using
a-zero based index and remember that you are accessing a whole array. Here
is an example:
using System;
public static class Exercise
{
static int Main(string[] args)
{
string[][] members = new string[2][];
members[0] = new string[] { "Celeste", "Mathurin", "Alex", "Germain" };
members[1] = new string[] { "Jeremy", "Mathew", "Anselme", "Frederique" };
foreach (string Name in members[0])
Console.WriteLine("Member: {0}", Name);
return 0;
}
}
This would produce:
Member: Celeste
Member: Mathurin
Member: Alex
Member: Germain
Press any key to continue . . .
To access the second array, apply its index as
ArrayName[1].
Application:
Using a Jagged Array
|
|
- To process the members of a jagged array, make the following changes
to the file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DepartmentStore3
{
public class DepartmentStore
{
static int Main(string[] args)
{
long ItemID = 0;
string Description = "Unknown";
double Price = 0.00D;
string Category = "Category";
// Each of the following variable
// arrays is structured as [2][2][4].
// Each variable represents:
// A/ Two major lists: The first major
// list represents women items,
// the second major list represents men items,
// B/ Two minor lists.
// Each of the major lists contains two minor lists:
// a/ The first minor list of the first major
// list contains adult women items
// The second minor list of the first major
// list contains girls items
// b/ The first minor list of the second major
// list contains adult men items
// The second minor list of the
// second major list contains boys items
// C/ Each minor list contains four items
long[][][] ItemNumber =
new long[][][]
{
new long[][]
{
new long[]{947783, 934687, 973947, 987598, 974937},
new long[]{743765, 747635, 765473, 754026, 730302}
},
new long[][]
{
new long[]{209579, 267583, 248937, 276057, 267945},
new long[]{ 409579, 467583, 448937, 476057, 467945}
}
};
string[][][] ItemName =
new string[][][]
{
new string[][]
{
new string[]
{
"Double-faced wool coat",
"Floral Silk Tank Blouse",
"Push Up Bra",
"Chiffon Blouse",
"Bow Belt Skirtsuit"
},
new string[]
{
"Cable-knit Sweater",
"Jeans with Heart Belt",
"Fashionable mini skirt",
"Double Dry Pants",
"Romantic Flower Dress"
}
},
new string[][]
{
new string[]
{
"Cotton Polo Shirt",
"Pure Wool Cap",
"Striped Cotton Shirt",
"Two-Toned Ribbed Crewneck",
"Chestnut Italian Shoes"
},
new string[]
{
"Under Collar and Placket Jacket",
"Country Coat Rugged Wear",
"Carpenter Jeans",
"Double-Cushion Tennis Shoes",
"Stitched Center-Bar Belt"
}
}
};
double[][][] UnitPrice =
new double[2][][]
{
new double[][]
{
new double[]
{ 275.25, 180.00, 50.00, 265.00, 245.55 },
new double[]
{ 45.55, 25.65, 34.55, 28.55, 24.95 }
},
new double[][]
{
new double[]
{ 45.75, 25.00, 65.55, 9.75, 165.75 },
new double[]
{ 265.15, 35.55, 24.95, 48.75, 32.50 }
}
};
// Order Processing
try
{
Console.Write("Enter Item Number: ");
ItemID = long.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine(
"Invalid Number - The program will terminate\n");
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 5; k++)
{
if (ItemID == ItemNumber[i][j][k])
{
Description = ItemName[i][j][k];
Price = UnitPrice[i][j][k];
if (ItemID >= 900000)
Category = "Women";
else if (ItemID >= 700000)
Category = "Girls";
else if (ItemID >= 400000)
Category = "Boys";
else
Category = "Men";
}
}
}
}
Console.WriteLine("Receipt");
Console.WriteLine("Item Number: {0}", ItemID);
Console.WriteLine("Description: {0}", Description);
Console.WriteLine("Unit Price: {0:C}\n", Price);
}
}
}
- To execute the application, Press F5
- Type the Item Number as 448937 and press Enter:
==============================
Fun Department Store
==============================
Receipt
------------------------------
Item Number: 448937
Description: Carpenter Jeans
Unit Price: $24.95
==============================
- Press Enter to close the DOS window
- On the main menu, click File -> Close Solution or File -> Close
Project
- When asked whether you want to save, click Discard
|
|