Bits & Bytes

Posts Tagged ‘array’

Using Rectangular and Jagged Arrays in C#

One of the unique and problematic features of C# is its usage of arrays. There are two different and distinct styles of syntax: comma delimited indices for rectangular arrays and repeated bracket operators for jagged arrays. These are written as “MyArray[2,4]” and “MyArray[2][4]”, respectively.

The program below demonstrates how to use both of these types of arrays by creating an example of each. Under the comment Rectangular Array Example, I declare and allocate a rectangular 2-dimensional array of stock price closes a few companies; the first dimension of the array is indexed by company and the second is indexed by the date. Under the comment Jagged Array Example, I declare and allocate a 2-dimensional jagged array of stock trades; the first dimension of the array is indexed by the ticker symbol and the second is indexed by the trade number for that symbol. These arrays could be of any number of dimensions; I have used the simple case of 2 dimensions for illustration.

For the first array, we have a 3 by 5 rectangular array of 5 stock price closes each for 3 companies. The entire 2-dimensional array is allocated all at once. Notice that indices are comma delimited, which is unusual for computer programming. Also, notice the nesting pattern for the braces where I have initialized the array entries. Inside the for-loops, I used the GetLength() function along with the dimensional index to get the length along any given dimension.

For the second array, we have 2 entries along the first dimension and 2 and 3 along the second dimension to specify 2 and 3 trades for each of the 2 symbols. The first dimension is allocated at the declaration; below that, each of the second dimensions are allocated and assigned values separately. The values of the arrays represent sells with positive numbers and buys with negative numbers. Inside the for-loops, I access the Length property to get the size along the first dimension. Then I access the Length property with each index by using the bracket operator to get the varying length along the second dimension. Notice that the more common repeated bracket syntax has replaced the comma delimited syntax in this second example.

The output from running the code is shown below the program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Arrays {
    class Program {
        static void Main(string[] args) {
            // ***** Rectangular Array Example *****
            // The stock price close for 3 companies for a week
            double[,] daaStockPriceClose = new double[3, 5]{
                {576.11, 585.00, 585.88, 583.82, 583.59},
                {44.94, 44.97, 45.34, 44.84, 45.35},
                {98.49, 99.41, 100.44, 100.57, 100.29}

            for (int iCompany = 0; iCompany < daaStockPriceClose.GetLength(0); ++iCompany) {
                Console.Write("Company #" + iCompany + ":  ");
                for (int iDay = 0; iDay < daaStockPriceClose.GetLength(1); ++iDay) {
                    Console.Write(daaStockPriceClose[iCompany ,iDay] + "  ");

            // ***** Jagged Array Example *****
            // The stock set of stock trades
            double[][] dppStockTrades = new double[2][];
            dppStockTrades[0] = new double[3]{10265.82, -4925.39, -3096.72};
            dppStockTrades[1] = new double[2]{6636.96, -9746.58};
            for (int iSymbol = 0; iSymbol < dppStockTrades.Length; ++iSymbol) {
                Console.Write("Symbol #" + iSymbol + ":  ");
                for (int iTrade = 0; iTrade < dppStockTrades[iSymbol].Length; ++iTrade) {
                    Console.Write("Trade #" + iTrade + ": " + dppStockTrades[iSymbol][iTrade] + "  ");


Declaring and Using Arrays in Actionscript 3.0

Arrays in Actionscript are extremely flexible; there are many ways to declare and assign values to an array in Actionscript. We can use functions, bracket notation, or constructors to create an Array object. We also have several ways to fill the entries of an Array object.

It is important to note that an Array in Actionscript is an object. So, an Actionscript Array is very different from say an array in C/C++. In fact, Arraysin Actionscript can hold many different types of data, which is different from arrays in most languages. However, Actionscript does also have a Vector type, which is similar to a traditional array in that, for efficiency, it is limited to holding only one type of data.

One dimensional Arrays are pretty straight-forward in how they can be declared and used. However, there are multiple methods for creating one-dimensional Arrays, which can be put together in numerous combinations to created multi-dimensional Array objects.

One-Dimensional Arrays

Function Notation

  1. var aMyArray:Array = Array();
  2. var aMyArray:Array = Array(3);
  3. var aMyArray:Array = Array(qMyObject);

Bracket Notation

  1. var aMyArray:Array = [“”, 17];

Contructor Notation

  1. var aMyArray:Array = new Array();
  2. var aMyArray:Array = new Array(3);
  3. var aMyArray:Array = new Array(1, 2, 3);

Above, we show several methods for creating an Array. The first three examples use function notation to create an Array of 0, 3, and 1 objects, respectively. Next, we have an Array of two elements that is created via the bracket notation; notice that the Array contains elements of different types: a string and an int. Lastly, we have three Array objects that are created using constructors: the first creates an Array with 0 elements, the second an Array with three elements, and the third an Array of the three elements 1, 2, and 3.

Filling an Array

  1. var aMyArray:Array = new Array();
    aMyArray.push(80, 120, 200, 440);
  2. var aMyArray:Array = new Array(4);
    aMyArray[0] = 80;
    aMyArray[1] = 120;
    aMyArray[2] = 200;
    aMyArray[3] = 440;

Above, we have two examples of how to declare and fill an Array with the four elements 80, 120, 200, and 400. In the first case, the push() function puts an element, or multiple elements, into an Array and extends the size of the Array. In the second case, we use the typical bracket operator [] to set the value of entries that are already allocated. There are many other functions available for removing elements, concatenating Arrays, and altering Arrays in many other different ways, as well; we will cover those later.

Multi-dimensional Arrays

  1. var aRow1:Array = new Array(1, 2, 3);
    var aRow2:Array = new Array(4, 5, 6);
    var aMyArray:Array = new Array(aRow1, aRow2);
  2. var aRow1:Array = [1, 2, 3];
    var aRow2:Array = [4, 5, 6];
    var aMyArray:Array = [aRow1, aRow2];
  3. var aMyArray:Array = new Array(2);
    aMyArray[0] = new Array(1, 2, 3);
    aMyArray[1] = new Array(4, 5, 6);
  4. var aMyArray:Array = new Array(2);
    aMyArray[0] = [1, 2, 3];
    aMyArray[1] = [4, 5, 6];
  5. var aMyArray:Array = [ [1, 2, 3], [4, 5, 6] ];

Here, we have 5 examples of how to create a two-dimensional Array object, by combining the methods for one-dimensional Arrays. We can continue like this and create Arrays with any number of dimensions.


© 2007–2022 LLC. All rights reserved.