Conteúdo do Curso
C# Basics
C# Basics
Practicing Multi-Dimensional Arrays
It is sometimes useful to declare constants for indexing multi-dimensional arrays. Following is the array from the last chapter's quiz:
main
const int FRUITS = 0; const int VEGETABLES = 1; string[,] foods = { { "Apple", "Apricot", "Banana", "Grapes", "Lime" }, // fruits { "Tomato", "Cabbage", "Carrot", "Pumpkin", "Broccoli" }, // vegetables };
We have created two constants FRUITS
and VEGETABLES
with values 0
and 1
, representing the rows 0
for fruits, and 1
for vegetables in the foods
array.
So if we need to access the "Cabbage" element from vegetables, we can simply write:
main
foods[VEGETABLES][1];
This trick might not be too helpful in smaller arrays like this one itself, however when dealing with more complex arrays, constants like these can help in easily accessing items from the array so that we don't need to memorize which row contains which items - in case we have categorized items in rows.
Now the challenge is to create a 2D array of type string
and name productOptions
, having a 4x3 shape (4 rows, 3 columns). Initialize it with the following data:
Row 1: "Red", "Green", "White" Row 2: "Square", "Rectangle", "Sphere" Row 3: "Thick", "Medium", "Thin" Row 4: "Printed", "Crafted", "None"
Access the "Crafted" element via indexing and output it. Use the relevant constant variable for that.
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { const int COLOR = 0; const int SHAPE = 1; const int WIDTH = 2; const int DESIGN = 3; // Create the array below _______ = { _______ }; // Reference the relevant element in the WriteLine statement Console.WriteLine(productOptions[DESIGN, 1]); Console.WriteLine(productOptions[COLOR, 2]); Console.WriteLine(productOptions[SHAPE, 0]); Console.WriteLine(productOptions[WIDTH, 1]); } } }
- For declaring a multidimensional array we add commas inside the square brackets (
[]
) depending upon the dimensions of the array. For-example, a 3D integer array would be declared likeint[,,] arrayName = { ... };
. - It is valid to use variable or constant values in indexing.
For-example if we have an array
int[] exampleArray = { 1, 2, 3, 4, 5 };
and a constantconst int someIndex = 2
, it is valid to writeexampleArray[someIndex]
for accessing the element 3 from the Array.
main
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { const int COLOR = 0; const int SHAPE = 1; const int WIDTH = 2; const int DESIGN = 3; // Create the array below string[,] productOptions = { { "Red", "Green", "White" }, { "Square", "Rectangle", "Sphere" }, { "Thick", "Medium", "Thin" }, { "Printed", "Crafted", "None" } }; // Reference the relevant element in the WriteLine statement Console.WriteLine(productOptions[DESIGN, 1]); Console.WriteLine(productOptions[COLOR, 2]); Console.WriteLine(productOptions[SHAPE, 0]); Console.WriteLine(productOptions[WIDTH, 1]); } } }
Obrigado pelo seu feedback!