Multidimensional Arrays and Classes/Records/Structures
Multidimensional Arrays and Classes/Records/Structures
Fundamentals of Multidimensional Arrays and Classes
Introduction
Like an array of a primitive type, a multidimensional array can be made a field or property of a class or record. You can primarily declare it without initializing it. Here is an example:
public class TriangleInCoordinateSystem
{
public int[,] Points;
}
This indicates a two-dimensional array field: the array will contain two lists but we don't (yet) know how many members each array will contain. Therefore, if you want, when creating the array, you can specify its dimension by assigning it a second pair of square brackets using the new operator and the data type. Here is an example:
internal class TriangleInCoordinateSystem
{
public int[,] Points = new int[3, 2];
}
You can also use a method of the class or record or a constructor to indicate the size of the array. Here is an example:
public class TriangleInCoordinateSystem { private int[,] points; public TriangleInCoordinateSystem() { Points = new int[3, 2]; } }
To initialize the array, you can access each member using the square brackets as we saw already. Here is an example:
public record TriangleInCoordinateSystem { public int[,] points; public TriangleInCoordinateSystem() { points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) } }
In the same way, you can access the members of the array. For example, you can display their values to the user. Here is an example:
using static System.Console; TriangleInCoordinateSystem tri = new TriangleInCoordinateSystem(); tri.ShowPoints(); Title = "Triangle Coordinates"; WriteLine("===================================="); public class TriangleInCoordinateSystem { private int[,] points; public TriangleInCoordinateSystem() { points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) } public void ShowPoints() { string strCoordinates = "Coordinates of the Triangle" + Environment.NewLine + "A(" + points[0, 0] + ", " + points[0, 1] + ")" + Environment.NewLine + "B(" + points[1, 0] + ", " + points[1, 1] + ")" + Environment.NewLine + "C(" + points[2, 0] + ", " + points[2, 1] + ")"; WriteLine(strCoordinates); } }
This would produce:
Coordinates of the Triangle A(-2, -3) B(5, 1) C(4, -2) ==================================== Press any key to close this window . . .
The Nullity of a Multidimensional Array
If you create a field as an array, the compiler would signal to you that the array may contain null values. To indicate that you are aware of it and will eventually address the issue by providing the necessary values, you can mark the array with a null-friendly operator. After doing that, when accessing the array, apply the null operator between the name of the array and the square brackets. This can be done as follows:
using static System.Console; TriangleInCoordinateSystem tri = new TriangleInCoordinateSystem(); tri.ShowPoints(); Title = "Triangle Coordinates"; WriteLine("===================================="); internal record class TriangleInCoordinateSystem { private int[,]? points; public TriangleInCoordinateSystem() { points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) } public void ShowPoints() { string strCoordinates = "Coordinates of the Triangle" + Environment.NewLine + "A(" + points?[0, 0] + ", " + points?[0, 1] + ")" + Environment.NewLine + "B(" + points?[1, 0] + ", " + points?[1, 1] + ")" + Environment.NewLine + "C(" + points?[2, 0] + ", " + points?[2, 1] + ")"; WriteLine(strCoordinates); } }
A Read-Only Multidimensional Array
When you are creating an array in a class, if you are planning to initialize it in the class, you can (and should) mark the array as read-only. To do this, apply the readonly operator to the variable. This can be done as follows:
using static System.Console;
TriangleInCoordinateSystem tri = new TriangleInCoordinateSystem();
tri.ShowPoints();
Title = "Triangle Coordinates";
WriteLine("====================================");
public record class TriangleInCoordinateSystem
{
readonly int[,] points;
public TriangleInCoordinateSystem()
{
points = new int[3, 2];
points[0, 0] = -2; // A(x, )
points[0, 1] = -3; // A( , y)
points[1, 0] = 5; // B(x, )
points[1, 1] = 1; // B( , y)
points[2, 0] = 4; // C(x, )
points[2, 1] = -2; // C( , y)
}
public void ShowPoints()
{
string strCoordinates = "Coordinates of the Triangle" + Environment.NewLine +
"A(" + points[0, 0] + ", " + points[0, 1] + ")" + Environment.NewLine +
"B(" + points[1, 0] + ", " + points[1, 1] + ")" + Environment.NewLine +
"C(" + points[2, 0] + ", " + points[2, 1] + ")";
WriteLine(strCoordinates);
}
}
Of course, a null-bound array can be created as read-only:
using static System.Console; TriangleInCoordinateSystem tri = new TriangleInCoordinateSystem(); tri.ShowPoints(); Title = "Triangle Coordinates"; WriteLine("===================================="); public record class TriangleInCoordinateSystem { private readonly int[,]? points; public TriangleInCoordinateSystem() { points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) } public void ShowPoints() { string strCoordinates = "Coordinates of the Triangle" + Environment.NewLine + "A(" + points?[0, 0] + ", " + points?[0, 1] + ")" + Environment.NewLine + "B(" + points?[1, 0] + ", " + points?1, 1] + ")" + Environment.NewLine + "C(" + points?[2, 0] + ", " + points?[2, 1] + ")"; WriteLine(strCoordinates); } }
A Multidimensional Array as Argument
A multidimensional array can be passed as argument. When creating the method, in its parentheses, enter the data type followed by the square brackets. In the square brackets, enter one comma for a two-dimensional array, two or more commas, as necessary, for a three-dimensional arrays as necessary. Here is an example:
public class TriangleInCoordinateSystem
{
public void ShowPoints(int[,] Coords)
{
}
}
When defining the method, in its body, you can use the array as you see fit, such as displaying its values.
To call this type of method, pass only the name of the array. Here is an example:
using static System.Console; int[,] points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) TriangleInCoordinateSystem tri = new TriangleInCoordinateSystem(); tri.ShowPoints(points); Title = "Triangle Coordinates"; WriteLine("===================================="); public class TriangleInCoordinateSystem { readonly int[,] points; public TriangleInCoordinateSystem() { points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) } public void ShowPoints(int[,] coords) { string strCoordinates = "Coordinates of the Triangle" + Environment.NewLine + "A(" + coords[0, 0] + ", " + coords[0, 1] + ")" + Environment.NewLine + "B(" + coords[1, 0] + ", " + coords[1, 1] + ")" + Environment.NewLine + "C(" + coords[2, 0] + ", " + coords[2, 1] + ")"; WriteLine(strCoordinates); } }
Returning a Multi-Dimensional Array
You can return a multi-dimensional array from a method. When creating the method, before its name, specify the data type followed by square brackets. In the square brackets, enter the necessary number of commas. Here is an example:
using System;
public class TriangleInCoordinateSystem
{
public int[,] CreateTriangle()
{
}
}
In the body of the method, you can do what you want but, before exiting it, you must return an array of the same type that was created. When calling the method, you can assign it to an array of the same type it returns. Here is an example:
using static System.Console; int[,] points = new int[3, 2]; TriangleInCoordinateSystem tri = new TriangleInCoordinateSystem(); points = tri.CreateTriangle(); tri.ShowPoints(points); WriteLine("==========================="); public class TriangleInCoordinateSystem { public int[,] CreateTriangle() { int[,] points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) return points; } public void ShowPoints(int[,] coords) { string strCoordinates = "Coordinates of the Triangle" + Environment.NewLine + "A(" + coords[0, 0] + ", " + coords[0, 1] + ")" + Environment.NewLine + "B(" + coords[1, 0] + ", " + coords[1, 1] + ")" + Environment.NewLine + "C(" + coords[2, 0] + ", " + coords[2, 1] + ")"; WriteLine(strCoordinates); } }
If you have a method that doesn't access instance variables, it is recommended that you create such a method as a static one. Of course, if you create a static method, any member you access from it will not need an instance variable. By applying staticity, the above program can be changed as follows::
using static System.Console; int[,] points = new int[3, 2]; TriangleInCoordinateSystem.CreateTriangle(); TriangleInCoordinateSystem.ShowPoints(points); WriteLine("==========================="); public class TriangleInCoordinateSystem { public static int[,] CreateTriangle() { int[,] points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) return points; } public static void ShowPoints(int[,] coords) { string strCoordinates = "Coordinates of the Triangle" + Environment.NewLine + "A(" + coords[0, 0] + ", " + coords[0, 1] + ")" + Environment.NewLine + "B(" + coords[1, 0] + ", " + coords[1, 1] + ")" + Environment.NewLine + "C(" + coords[2, 0] + ", " + coords[2, 1] + ")"; WriteLine(strCoordinates); } }
Passing a Multi-Dimensional Array by Reference
When passing a multi-dimensional array as argument, you can pass the array as a reference. This makes it possible for the method to modify the array and return it changed. If you want to indicate that the array is passed by reference, precede its data type in the parentheses by the the ref keyword. Here is an example:
using static System.Console; int[,] pts = new int[3, 2]; TriangleInCoordinateSystem tri = new TriangleInCoordinateSystem(ref pts); tri.ShowPoints(pts); Title = "Triangle Coordinates"; WriteLine("===================================="); internal record TriangleInCoordinateSystem { public TriangleInCoordinateSystem(ref int[,] points) { points = new int[3, 2]; points[0, 0] = -2; // A(x, ) points[0, 1] = -3; // A( , y) points[1, 0] = 5; // B(x, ) points[1, 1] = 1; // B( , y) points[2, 0] = 4; // C(x, ) points[2, 1] = -2; // C( , y) } public void ShowPoints(int[,] coords) { string strCoordinates = "Coordinates of the Triangle" + Environment.NewLine + "A(" + coords[0, 0] + ", " + coords[0, 1] + ")" + Environment.NewLine + "B(" + coords[1, 0] + ", " + coords[1, 1] + ")" + Environment.NewLine + "C(" + coords[2, 0] + ", " + coords[2, 1] + ")"; WriteLine(strCoordinates); } }
A Multidimensional Array of Objects
A Variable of a Multidimensional Array of Objects
As done for primitive data types, you can create a multi-dimensional array where each member is of a class, record, or structure type. You can use an existing class (or structure or record) or create a class, a record, or a structure. Here is an example of a class:
public record Coordinate { public int X { get; set; } public int Y { get; set; } }
To create a multidimensional array of objects without initializing it, you can use the following formula:
class-record-structure-name[,] variable-name;
If you know the number of instances of the class (or record or structure) that the array will use, you can use the following formula:
class-record-structure-name[,] variable-name = new class-record-structure-name[number1,number2];
You can use the var or the dynamic keyword in the following formula:
var variable-name = new data-type[number1,number2]; dynamic variable-name = new data-type[number1,number2];
The class-record-structure-name is the name of the class, record, or structure that will represent each member of the array. The other sections follow the same rules we reviewed for the primitive types. For example, you can create an array without allocating memory for it as follows:
Coordinate[,] line;
If you know the number of members that the array will contain, you can use the right pair of square brackets, in which case you can specify the name of the class or use the var keyword and omit the left square brackets. Here is an example:
Coordinate line = new Coordinate[2, 2];
This declaration creates a two-dimensional array of two Coordinate objects: The array contains two lists and each list contains two Coordinate objects.
To initialize a multidimensional array of objects, you can access each array member using its index, and allocate memory for it using the new operator. After allocating memory for the member, you can then access its fields or properties to initialize it. Here is an example:
Coordinate[,] line = new Coordinate[2, 2]; line[0, 0] = new Coordinate(); // First Coordinate A line[0, 0].X = -3; // A(x, ) line[0, 0].Y = 8; // A( , y) line[0, 1] = new Coordinate(); // Second Coordinate B line[0, 1].X = 4; // B(x, ) line[0, 1].Y = -5; // B( , y) public class Coordinate { public int X { get; set; } public int Y { get; set; } }
You can also initialize the array when creating it. Before doing this, you would need a constructor of the class and the constructor must take the argument(s) that would be used to initialize each member of the array. To actually initialize the array, you would need a pair of external curly brackets for the main array. Inside of the external curly brackets, create a pair of curly brackets for each sub-dimension of the array. Inside the last curly brackets, use the new operator to access an instance of the class and call its constructor to specify the values of the instance of the class. Here is an example:
Coordinate[,] line = new Coordinate[,] { { new Coordinate(-3, 8), new Coordinate( 4, -5) } }; public struct Coordinate { public int X { get; init; } public int Y { get; init; } public Coordinate(int x, int y) { X = x; Y = y; } }
Accessing the Members of a Multidimensional Array of Objects
To access the members of a multidimensional array of objects, apply the square brackets to a particular member of the array and access the desired field or property of the class. Here is an example:
using static System.Console; using static System.Environment; Title = "Triangle Coordinates"; Coordinate[,] line = new Coordinate[,] { { new Coordinate(-3, 8), new Coordinate(4, -5) } }; WriteLine("Line =-=" + NewLine + "From A(" + line[0, 0].X + ", " + line[0, 0].Y + " to B(" + line[0, 1].X + ", " + line[0, 1].Y + ")"); WriteLine("==========================="); public struct Coordinate { public int X { get; init; } public int Y { get; init; } public Coordinate(int x, int y) { X = x; Y = y; } }
This would produce:
Line =-= From A(-3, 8 to B(4, -5) =========================== Press any key to close this window . . .
You can also use a loop to access the members of the array. To do this, create a first for loop that stops at the first dimension of the array - 1. Then, inside of a first for loop, nest a for loop for each subsequent dimension. Here is an example:
using static System.Console; using static System.Environment; Title = "Geometry"; Coordinate[,] line = new Coordinate[,] { { new Coordinate(-3, 8), new Coordinate(4, -5) } }; string strLine = "The points of the line are:" + NewLine; for (int i = 0; i < 1; i++) for (int j = 0; j < 2; j++) strLine += "(" + line[i, j].X + ", " + line[i, j].Y + ")" + NewLine; WriteLine(strLine); WriteLine("==========================="); internal record class Coordinate { public int X { get; init; } public int Y { get; init; } public Coordinate(int x, int y) { X = x; Y = y; } }
This would produce:
The points of the line are: (-3, 8) (4, -5) =========================== Press any key to close this window . . .
To apply a foreach operator, access only each member of the internal list. Here is an example:
using static System.Console; using static System.Environment; Title = "Geometry"; Coordinate[,] line = new Coordinate[,] { { new Coordinate(-3, 8), new Coordinate(4, -5) } }; string strLine = "The points of the line are:" + NewLine; foreach (Coordinate coord in line) strLine += "(" + coord.X + ", " + coord.Y + ")" + NewLine; WriteLine(strLine); WriteLine("==========================="); internal record struct Coordinate { public int X { get; init; } public int Y { get; init; } public Coordinate(int x, int y) { X = x; Y = y; } }
Multidimensional Arrays of Objects and Classes/Records
Introduction
As done for primitive types, a multidimensional array of objects can be made a field of a class, record, or structure. You can declare the array without specifying its size. Here is an example:
public class Triangle
{
public Coordinate[,] vertices;
}
If you know the dimensions that the array will have, you can specify them using the new operator at the same time you are creating the field. Here is an example:
public class Triangle
{
public Coordinate[,] vertices = new Coordinate[3,2];
}
This creation signals a multidimensional array of Coordinate objects. It will consist of three lists and each list will contain two Coordinate objects
To initialize the array, access each member by its index to allocate memory for it. Once you get the member, you access each its fields or properties and initialize each with the desired value. Here is an example:
public struct Triangle { public Coordinate[,] vertices = new Coordinate[3, 2]; public Triangle() { vertices[0, 0] = new Coordinate(); // Coordinate A(x, y) vertices[0, 0].x = -2; // A(x, ) vertices[0, 0].y = -4; // A( , y) vertices[1, 0] = new Coordinate(); // Coordinate B(x, y) vertices[1, 0].x = 3; // B(x, ) vertices[1, 0].y = 5; // B( , y) vertices[2, 0] = new Coordinate(); // Point C(x, y) vertices[2, 0].x = 6; // C(x, ) vertices[2, 0].y = -2; // C( , y) } }
If the class is equipped with the appropriate constructor, you can use it to initialize each member of the array.
Once the array is ready, you can access each members using its index and manipulate it. For example you can display its value(s) to the user. Here is an example:
using static System.Console; using static System.Environment; Title = "Geometry"; Triangle tri = new Triangle(); tri.Identify(); WriteLine("================================="); public struct Coordinate { public int X { get; set; } public int Y { get; set; } public Coordinate() { X = 0; Y = 0; } public Coordinate(int x, int y) { X = x; Y = y; } } public struct Triangle { public Coordinate[,] vertices = new Coordinate[3, 2]; public Triangle() { vertices[0, 0] = new Coordinate(); // Point A(x, y) vertices[0, 0].X = -2; // A(x, ) vertices[0, 0].Y = -4; // A( , y) vertices[1, 0] = new Coordinate(); // Point B(x, y) vertices[1, 0].X = 3; // B(x, ) vertices[1, 0].Y = 5; // B( , y) vertices[2, 0] = new Coordinate(); // Point C(x, y) vertices[2, 0].X = 6; // C(x, ) vertices[2, 0].Y = -2; // C( , y) } public void Identify() { string strTriangle = "Triangle Vertices: " + NewLine + "A(" + vertices[0, 0].X + ", " + vertices[0, 0].Y + "), B(" + vertices[1, 0].X + ", " + vertices[1, 0].Y + "), and C(" + vertices[2, 0].X + ", " + vertices[2, 0].Y + ")"; WriteLine(strTriangle); } }
This would produce:
Triangle Vertices: A(-2, -4), B(3, 5), and C(6, -2) ================================= Press any key to close this window . . .
Passing a Multidimensional Array of Objects
You can pass a multidimensional array of objects as arguments. To do this, in the parentheses of the method, enter the class, record, or structure name followed by the square brackets. In the square brackets, type the appropriate number of commas. Here is an example:
public class Triangle
{
public void Create(Coordinate[,] points)
{
}
}
In the body of the method, use the array as you we have done so far. You can access its members to get to its values. Here are examples:
using static System.Console;
using static System.Environment;
Title = "Geometry";
Triangle tri = new Triangle();
Coordinate[,] coords = new Coordinate[3, 2];
tri.Create(coords);
tri.Identify(coords);
WriteLine("=================================");
public record struct Coordinate
{
public int X { get; set; }
public int Y { get; set; }
}
internal record class Triangle
{
public void Create(Coordinate[,] points)
{
points[0, 0] = new Coordinate(); // Point A(x, y)
points[0, 0].X = 1; // A(x, )
points[0, 0].Y = 5; // A( , y)
points[1, 0] = new Coordinate(); // Point B(x, y)
points[1, 0].X = -3; // B(x, )
points[1, 0].Y = 2; // B( , y)
points[2, 0] = new Coordinate(); // Point C(x, y)
points[2, 0].X = 4; // C(x, )
points[2, 0].Y = -1; // C( , y)
}
public void Identify(Coordinate[,] vertices)
{
string strTriangle = "Triangle Vertices: " + NewLine +
"A(" + vertices[0, 0].X + ", " + vertices[0, 0].Y +
"), B(" + vertices[1, 0].X + ", " + vertices[1, 0].Y +
"), and C(" + vertices[2, 0].X + ", " + vertices[2, 0].Y + ")";
WriteLine(strTriangle);
}
}
The above code would produce:
Triangle Vertices: A(1, 5), B(-3, 2), and C(4, -1) ================================= Press any key to close this window . . .
Returning a Multidimensional Array of Objects
A method can return a multidimensional array of objects. If you are creating the method, before its name, type the name of the class followed by square brackets. Inside the square brackets, type the desired number of commas to indicate the dimension of the returned value. Here is an example:
public class Triangle
{
public Coordinate[, ] Create()
{
}
}
After implementing the method, before exiting it, make sure it returns the type of array that it was indicated to produce. Here is an example:
using static System.Console; using static System.Environment; Title = "Geometry - Triangle Coordinates"; Triangle tri = new Triangle(); Coordinate[,] coords = tri.Create(); tri.Identify(coords); WriteLine("================================="); public record struct Coordinate { public int X { get; set; } public int Y { get; set; } } internal record class Triangle { public Coordinate[,] Create() { Coordinate[,] points = new Coordinate[3, 2]; points[0, 0] = new Coordinate(); // Point A(x, y) points[0, 0].X = -2; // A(x, ) points[0, 0].Y = -4; // A( , y) points[1, 0] = new Coordinate(); // Point B(x, y) points[1, 0].X = 3; // B(x, ) points[1, 0].Y = 5; // B( , y) points[2, 0] = new Coordinate(); // Point C(x, y) points[2, 0].X = 6; // C(x, ) points[2, 0].Y = -2; // C( , y) return points; } public void Identify(Coordinate[,] vertices) { string strTriangle = "Triangle Vertices: " + NewLine + "A(" + vertices[0, 0].X + ", " + vertices[0, 0].Y + "), B(" + vertices[1, 0].X + ", " + vertices[1, 0].Y + "), and C(" + vertices[2, 0].X + ", " + vertices[2, 0].Y + ")"; WriteLine(strTriangle); } }
This would produce:
Triangle Vertices: A(-2, -4), B(3, 5), and C(6, -2) ================================= Press any key to close this window . . .
Passing an Array by Reference
When an array of objects is passed normally, when the method ends, the changes made to the array are kept. This demonstrates that an array of objects is passe by reference. If you want to indicate that you are passing the array by referrence, precede the data type of the parameter with the ref keyword. Here is an example:
using static System.Console; using static System.Environment; Title = "Geometry"; Triangle tri = new Triangle(); Coordinate[,] coords = new Coordinate[3, 2]; tri.Create(ref coords); tri.Identify(coords); WriteLine("================================="); public record struct Coordinate { public int X { get; set; } public int Y { get; set; } } internal record class Triangle { public void Create(ref Coordinate[,] points) { points[0, 0] = new Coordinate(); // Point A(x, y) points[0, 0].X = 4; // A(x, ) points[0, 0].Y = 1; // A( , y) points[1, 0] = new Coordinate(); // Point B(x, y) points[1, 0].X = -4; // B(x, ) points[1, 0].Y = -2; // B( , y) points[2, 0] = new Coordinate(); // Point C(x, y) points[2, 0].X = 2; // C(x, ) points[2, 0].Y = 5; // C( , y) } public void Identify(Coordinate[,] vertices) { string strTriangle = "Triangle Vertices: " + NewLine + "A(" + vertices[0, 0].X + ", " + vertices[0, 0].Y + "), B(" + vertices[1, 0].X + ", " + vertices[1, 0].Y + "), and C(" + vertices[2, 0].X + ", " + vertices[2, 0].Y + ")"; WriteLine(strTriangle); } }
This would produce:
Triangle Vertices: A(4, 1), B(-4, -2), and C(2, 5) ================================= Press any key to close this window . . .
Jagged Arrays and Classes
Introduction
A jagged array of objects is an array where each element holds its own array of objects. Once again, all objects must be of the same type. This means that you must first have the class, a record, or a structure on which each object will be based. you can use one of the many .NET Framework built-in classes or you can create your own class, record, or structure.
The formula to create a jagged array of objects is the same for primitive types. Specify the number of square brackets after the class, record, or structure name on the left side of the variable name. On the right side of the = operator, use the same number of square brackets. In the first brackets, specify the number of arrays. Here is an example:
State[][] states = new State[2][];
public class State
{
public string? StateName { get; set; }
public double Area { get; set; }
public string? Capital { get; set; }
}
You must then define each array by accessing its index and initialize each. Each array can have a different length. Here are starting examples:
State[][] states = new State[3][]; // United States states[0] = new State[50]; // Australia states[1] = new State[6]; // Germany states[2] = new State[16]; public class State { public string? StateName { get; set; } public double Area { get; set; } public string? Capital { get; set; } }
By accessing an array, you can initialize it using any of the techniques we have used so far. For example, to access the first array, apply index 0 in the first square brackets and the array's own index in the second square bracket. From there, access the member of the class. If it is a field or a property, you can assign the desired value to it. This can be done as follows:
states[0] = new State[6];
states[0][0] = new State();
states[0][0].StateName = "Western Australia (WA)";
states[0][0].Area = 2645615f;
states[0][0].Capital = "Perth";
states[0][1] = new State();
states[0][1].StateName = "South Australia (SA)";
states[0][1].Area = 1043514f;
states[0][1].Capital = "Adelaide";
You can also initialize the whole array in a delimited curly bracket. Here is an example:
State[][] states = new State[2][];
// Australia
states[0] = new State[6];
states[0][0] = new State();
states[0][0].StateName = "Western Australia (WA)";
states[0][0].Area = 2645615f;
states[0][0].Capital = "Perth";
states[0][1] = new State();
states[0][1].StateName = "South Australia (SA)";
states[0][1].Area = 1043514f;
states[0][1].Capital = "Adelaide";
states[0][2] = new State();
states[0][2].StateName = "Queensland (QLD)";
states[0][2].Area = 1852642f;
states[0][2].Capital = "Brisbane";
states[0][3] = new State();
states[0][3].StateName = "New South Wales (NSW)";
states[0][3].Area = 809444f;
states[0][3].Capital = "Sydney";
states[0][4] = new State();
states[0][4].StateName = "Victoria (VIC)";
states[0][4].Area = 237639f;
states[0][4].Capital = "Melbourne";
states[0][5] = new State();
states[0][5].StateName = "Tasmania (TAS)";
states[0][5].Area = 68401f;
states[0][5].Capital = "Hobart";
// Germany
states[1] = new State[]
{
new State() { StateName = "Saxony", Area = 18415.66f, Capital = "Dresden" },
new State() { StateName = "Lower Saxony", Area = 47614.07f, Capital = "Hanover" },
new State() { StateName = "Saxony-Anhalt", Area = 20451.58f, Capital = "Magdeburg" },
new State() { StateName = "Saarland", Area = 2570f, Capital = "Saarbrücken" },
new State() { StateName = "North Rhine-Westphalia", Area = 34084.13f, Capital = "Düsseldorf" },
new State() { StateName = "Berlin", Area = 891.70f, Capital = "Berlin" },
new State() { StateName = "Thuringia", Area = 16171f, Capital = "Erfurt" },
new State() { StateName = "Baden-Württemberg", Area = 35751.46f, Capital = "Stuttgart" },
new State() { StateName = "Hamburg", Area = 755f, Capital = "Hamburg" },
new State() { StateName = "Rhineland-Palatinate", Area = 19854.21f, Capital = "Mainz" },
new State() { StateName = "Schleswig-Holstein", Area = 15763.18f, Capital = "Kiel" },
new State() { StateName = "Brandenburg", Area = 29478.63f, Capital = "Potsdam" },
new State() { StateName = "Bavaria", Area = 70549.44f, Capital = "Munich" },
new State() { StateName = "Bremen ", Area = 419.38f, Capital = "Bremen" },
new State() { StateName = "Hesse", Area = 21100f, Capital = "Wiesbaden" },
new State() { StateName = "Mecklenburg-Vorpommern", Area = 15763.18f, Capital = "Schwerin" }
};
public class State
{
public string? StateName { get; set; }
public float Area { get; set; }
public string? Capital { get; set; }
}
Accessing the Objects of a Jagged Array of Objects
Since a jagged array is an array of arrays, remember that each array has its own number of items. This means that you have many options to access the members of a jagged array. To start, to access one individual primary array, you can first specify its index in the left square brackets. From there, you can use the second square brackets to access an item of that particular array. Here is an example:
using static System.Console;
using static System.Environment;
Title = "Countries Statistics";
State[][] states = new State[2][];
// Australia
states[0] = new State[6];
states[0][0] = new State();
states[0][0].StateName = "Western Australia (WA)";
states[0][0].Area = 2645615f;
states[0][0].Capital = "Perth";
states[0][1] = new State();
states[0][1].StateName = "South Australia (SA)";
states[0][1].Area = 1043514f;
states[0][1].Capital = "Adelaide";
states[0][2] = new State();
states[0][2].StateName = "Queensland (QLD)";
states[0][2].Area = 1852642f;
states[0][2].Capital = "Brisbane";
states[0][3] = new State();
states[0][3].StateName = "New South Wales (NSW)";
states[0][3].Area = 809444f;
states[0][3].Capital = "Sydney";
states[0][4] = new State();
states[0][4].StateName = "Victoria (VIC)";
states[0][4].Area = 237639f;
states[0][4].Capital = "Melbourne";
states[0][5] = new State();
states[0][5].StateName = "Tasmania (TAS)";
states[0][5].Area = 68401f;
states[0][5].Capital = "Hobart";
// Germany
states[1] = new State[]
{
new State() { StateName = "Saxony", Area = 18415.66f, Capital = "Dresden" },
new State() { StateName = "Lower Saxony", Area = 47614.07f, Capital = "Hanover" },
new State() { StateName = "Saxony-Anhalt", Area = 20451.58f, Capital = "Magdeburg" },
new State() { StateName = "Saarland", Area = 2570f, Capital = "Saarbrücken" },
new State() { StateName = "North Rhine-Westphalia", Area = 34084.13f, Capital = "Düsseldorf" },
new State() { StateName = "Berlin", Area = 891.70f, Capital = "Berlin" },
new State() { StateName = "Thuringia", Area = 16171f, Capital = "Erfurt" },
new State() { StateName = "Baden-Württemberg", Area = 35751.46f, Capital = "Stuttgart" },
new State() { StateName = "Hamburg", Area = 755f, Capital = "Hamburg" },
new State() { StateName = "Rhineland-Palatinate", Area = 19854.21f, Capital = "Mainz" },
new State() { StateName = "Schleswig-Holstein", Area = 15763.18f, Capital = "Kiel" },
new State() { StateName = "Brandenburg", Area = 29478.63f, Capital = "Potsdam" },
new State() { StateName = "Bavaria", Area = 70549.44f, Capital = "Munich" },
new State() { StateName = "Bremen ", Area = 419.38f, Capital = "Bremen" },
new State() { StateName = "Hesse", Area = 21100f, Capital = "Wiesbaden" },
new State() { StateName = "Mecklenburg-Vorpommern", Area = 15763.18f, Capital = "Schwerin" }
};
for (int i = 0; i < 6; i++)
WriteLine("State: " + states[0][i].StateName, "States Statistics - Australia");
WriteLine("=================================");
public class State
{
public string? StateName { get; set; }
public float Area { get; set; }
public string? Capital { get; set; }
}
This would produce:
State: Western Australia (WA) State: South Australia (SA) State: Queensland (QLD) State: New South Wales (NSW) State: Victoria (VIC) State: Tasmania (TAS) ================================= Press any key to close this window . . .
You would use the same technique to access each primary array. Since the primary arrays may have different numbers of internal arrays, you can use a conditional statement to find out what array you are accessing at one particular time. Here is an example:
using static System.Console; Title = "Countries Statistics"; State[][] states = new State[2][]; // Australia states[0] = new State[6]; states[0][0] = new State(); states[0][0].Abbreviation = "WA "; states[0][0].StateName = "Western Australia "; states[0][0].Area = 2645615f; states[0][0].Capital = "Perth"; states[0][1] = new State(); states[0][1].Abbreviation = "SA "; states[0][1].StateName = "South Australia "; states[0][1].Area = 1043514f; states[0][1].Capital = "Adelaide"; states[0][2] = new State(); states[0][2].Abbreviation = "QLD"; states[0][2].StateName = "Queensland "; states[0][2].Area = 1852642f; states[0][2].Capital = "Brisbane"; states[0][3] = new State(); states[0][3].Abbreviation = "NSW"; states[0][3].StateName = "New South Wales "; states[0][3].Area = 809444f; states[0][3].Capital = "Sydney"; states[0][4] = new State(); states[0][4].Abbreviation = "VIC"; states[0][4].StateName = "Victoria "; states[0][4].Area = 237639f; states[0][4].Capital = "Melbourne"; states[0][5] = new State(); states[0][5].Abbreviation = "TAS"; states[0][5].StateName = "Tasmania "; states[0][5].Area = 68401f; states[0][5].Capital = "Hobart"; // Germany states[1] = new State[] { new State() { StateName = "Saxony ", Area = 18415.66f, Capital = "Dresden" }, new State() { StateName = "Lower Saxony ", Area = 47614.07f, Capital = "Hanover" }, new State() { StateName = "Saxony-Anhalt ", Area = 20451.58f, Capital = "Magdeburg" }, new State() { StateName = "Saarland ", Area = 2570f, Capital = "Saarbrücken" }, new State() { StateName = "North Rhine-Westphalia", Area = 34084.13f, Capital = "Düsseldorf" }, new State() { StateName = "Berlin ", Area = 891.70f, Capital = "Berlin" }, new State() { StateName = "Thuringia ", Area = 16171f, Capital = "Erfurt" }, new State() { StateName = "Baden-Württemberg ", Area = 35751.46f, Capital = "Stuttgart" }, new State() { StateName = "Hamburg ", Area = 755f, Capital = "Hamburg" }, new State() { StateName = "Rhineland-Palatinate ", Area = 19854.21f, Capital = "Mainz" }, new State() { StateName = "Schleswig-Holstein ", Area = 15763.18f, Capital = "Kiel" }, new State() { StateName = "Brandenburg ", Area = 29478.63f, Capital = "Potsdam" }, new State() { StateName = "Bavaria ", Area = 70549.44f, Capital = "Munich" }, new State() { StateName = "Bremen ", Area = 419.38f, Capital = "Bremen" }, new State() { StateName = "Hesse ", Area = 21100f, Capital = "Wiesbaden" }, new State() { StateName = "Mecklenburg-Vorpommern", Area = 15763.18f, Capital = "Schwerin" } }; WriteLine("Countries Statistics"); WriteLine("==============================================================="); WriteLine(" # Abbrv State Name Area Capital"); WriteLine("==============================================================="); for (int i = 0; i < 2; i++) { int counter = 1; if (i == 0) { for (int j = 0; j < 6; j++) { WriteLine($"{counter,4} {states[0][j].Abbreviation} {states[0][j].StateName} {states[0][j].Area,10} {states[0][j].Capital}"); counter++; } } else // if( i == 1) { counter = 1; for (int j = 0; j < 16; j++) { WriteLine($"{counter,4} {states[1][j].StateName} {states[1][j].Area,10} {states[1][j].Capital}"); counter++; } } } WriteLine("==============================================================="); public class State { public string? Abbreviation { get; set; } public string? StateName { get; set; } public float Area { get; set; } public string? Capital { get; set; } }
This would produce:
Countries Statistics =============================================================== # Abbrv State Name Area Capital =============================================================== 1 WA Western Australia 2645615 Perth 2 SA South Australia 1043514 Adelaide 3 QLD Queensland 1852642 Brisbane 4 NSW New South Wales 809444 Sydney 5 VIC Victoria 237639 Melbourne 6 TAS Tasmania 68401 Hobart 1 Saxony 18415.66 Dresden 2 Lower Saxony 47614.07 Hanover 3 Saxony-Anhalt 20451.58 Magdeburg 4 Saarland 2570 Saarbrücken 5 North Rhine-Westphalia 34084.13 Düsseldorf 6 Berlin 891.7 Berlin 7 Thuringia 16171 Erfurt 8 Baden-Württemberg 35751.46 Stuttgart 9 Hamburg 755 Hamburg 10 Rhineland-Palatinate 19854.21 Mainz 11 Schleswig-Holstein 15763.18 Kiel 12 Brandenburg 29478.63 Potsdam 13 Bavaria 70549.44 Munich 14 Bremen 419.38 Bremen 15 Hesse 21100 Wiesbaden 16 Mecklenburg-Vorpommern 15763.18 Schwerin =============================================================== Press any key to close this window . . .
|
|||
Previous | Copyright © 2008-2024, FunctionX | Friday 26 November 2021 | Next |
|