Jagged Arrays |
|
A jagged array is just 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: long[2][5] Distances; |
This declares a variable that represents two arrays and each array internally contains 5 arrays. The first square bracket can also be used as its own multidimensional array. Here is an example: long[2,4][5] Distances; In the same way, the second square bracket can be used as a single or a multidimensional array. Here is an example: long[2,4][5,12,8] Distances;
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 square of the 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; namespace CSharpLessons { class Exercise { static void Main() { string[][] Members = new string[2][]; } } } 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; namespace CSharpLessons { class Exercise { static void Main() { string[][] Members = new string[2][]{ new string[]{"Celeste", "Mathurin", "Alex", "Germain"}, new string[]{"Jeremy", "Mathew", "Anselme", "Frederique"} }; Console.WriteLine(); } } } 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: |
namespace CSharpLessons { class Exercise { static void Main() { string[][] Members = new string[2][]; Members[0] = new string[]{"Celeste", "Mathurin", "Alex", "Germain"}; Members[1] = new string[]{"Jeremy", "Mathew", "Anselme", "Frederique"}; Console.WriteLine(); } } }
Practical Learning: Initializing a Jagged Array |
using System; namespace DepartmentStore5 { class Program { static int Main(string[] args) { long ItemID = 0; string Description = "Unknown"; decimal Price = 0.00M; // 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" } } }; decimal[][][] UnitPrice = new decimal[2][][] { new decimal[][] { new decimal[]{ 275.25M, 180.00M, 50.00M, 265.00M, 245.55M }, new decimal[]{ 45.55M, 25.65M, 34.55M, 28.55M, 24.95M } }, new decimal[][] { new decimal[]{ 45.75M, 25.00M, 65.55M, 9.75M, 165.75M }, new decimal[]{ 265.15M, 35.55M, 24.95M, 48.75M, 32.50M } } }; Console.WriteLine("Receipt"); Console.WriteLine("Item Number: {0}", ItemID); Console.WriteLine("Description: {0}", Description); Console.WriteLine("Unit Price: {0:C}\n", Price); return 0; } } } |
Access to Members of a Jagged Array |
As done for a multidimensional array, each member of a jagged array can be access 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; namespace CSharpLessons { class Exercise { static void Main() { 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]); } } }
Practical Learning: Using a Jagged Array |
using System; namespace DepartmentStore5 { class Program { static int Main(string[] args) { long ItemID = 0; string Description = "Unknown"; decimal Price = 0.00M; 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" } } }; decimal[][][] UnitPrice = new decimal[2][][] { new decimal[][] { new decimal[]{ 275.25M, 180.00M, 50.00M, 265.00M, 245.55M }, new decimal[]{ 45.55M, 25.65M, 34.55M, 28.55M, 24.95M } }, new decimal[][] { new decimal[]{ 45.75M, 25.00M, 65.55M, 9.75M, 165.75M }, new decimal[]{ 265.15M, 35.55M, 24.95M, 48.75M, 32.50M } } }; // 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("Category: {0}", Category); Console.WriteLine("Description: {0}", Description); Console.WriteLine("Unit Price: {0:C}\n", Price); return 0; } } } |
Enter Item Number: 448937 Receipt Item Number: 448937 Category: Boys Description: Carpenter Jeans Unit Price: $24.95 Press any key to continue . . . |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|