Today, I'm going to start a new series of posts called "Arrays and Indexers in [Programming Language]". As with previous posts, my aim is to show programming language's basic features, in this case, Arrays and Indexers.
There will a little program. It will show the different declaration and initialization syntax for single-dimensional, multi-dimensional and jagged (arrays of arrays) arrays, how to access elements using numerical index, how to use them as method's input parameters and return type. For the last example, I will define a class that implements the Indexer with its respective get set properties/methods and use it.
Regarding the demonstrating code, I chose an easy array-based sorting algorithm "BubbleSort" (single-dimensional array), a Matrix Transpose (multi-dimensional array) and a Random Uppercase Array. There will also be some Print Array Content methods to display the elements of the array. For the indexer part, it will just encapsulate an array of characters to store alphabets and access specific letters on it.
Each post of the series will be implemented in the latest version, available at the time of writing (stable or unstable), of the following languages targeting the latest version of the supported stable Runtime (JDK7 & .NET4.5)
CLR: C#, VB.NET, C++/CLI, F#, Boo, Phalanger, IronPython, IronRuby, Oxygene, Zonnon, Nemerle, Cobra, JScript.NET.
JVM: Java, Groovy, Jython, JRuby, Fantom, Scala, Gosu, Ceylon, Xtend, Kotlin.
And will use its respective System.Array or java.util.Array.
The Program's Structure will be (more or less) as follows:
// imports // namespace // Program Class // Main Mathod // Single-dimensional Array(s) // Declare & Initialize Character Array // Reverse Array Elements // Print Array // Declare & Initialize Integer Array // Sort Integer Array Elements // Print Array // Declare & Initialize String Array // Sort String Array Elements // Print Array // Multi-dimensional Array (Matrix row,column) // Declare & Initialize Matrix Array // Transpose Matrix // Print Array // Jagged Array (array of array) // Upper Case Random Array // Print Array // Graph Number of Elements // Common Array Exceptions // Print Exceptions // Indexers Usage // Declare & Initialize // Access Class Array Elements via Indexer // Method: ReverseChar - Char // Method: BubbleSortInt - Integer // Method: BubbleSortString - String // Method: TransposeMatrix - Integer[,] // Method: UpperCaseRandomArray - String // Method: PrintArrayChar - Char // Method: PrintArrayInt - Integer // Method: PrintArrayString - String // Method: PrintMatrix - Integer[,] // Method: GraphJaggedArray - String // Method: PrintJaggedArray - String // Method: PrintCommonArrayExceptions - String // Method: PrintTitle // Indexer Class // Field: Char Array // Indexer // Get // Set // Constructor: - Integer // Constructor: - String // Constructor: - Char Array // Method: toString // Method: Slice
Here below some definitions of the new concepts:
"An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched."
Taken from: http://www.techterms.com/definition/array
"A one-dimensional array (or single dimension array) is a type of linear array. Accessing its elements involves a single subscript which can either represent a row or column index."
Taken from: http://en.wikipedia.org/wiki/One-dimensional_array#One-dimensional_arrays
"A data structure consisting of a vector of vectors, in the case of a 2-dimensional array, or, in the case of an N-dimensional array, a vector of multidimensional arrays of degree N minus 1, thereby allowing the simulation of a N-dimensional grid of storage locations using an underlying memory architecture in which storage is addressed in a linear fashion."
Taken from: http://en.wiktionary.org/wiki/multidimensional_array
"In computer programming, an Iliffe vector (Jagged Arrays or Array of Arrays), also known as a display, is a data structure used to implement multi-dimensional arrays. An Iliffe vector for an n-dimensional array (where n > 2) consists of a vector (or 1-dimensional array) of pointers to an (n - 1)-dimensional array. They are often used to avoid the need for expensive multiplication operations when performing address calculation on an array element."
Taken from: http://en.wikipedia.org/wiki/Jagged_array
Bubble Sort Algorithm
"Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted."
Sorting Algorithm in Action: http://www.sorting-algorithms.com/bubble-sort
Taken from: http://en.wikipedia.org/wiki/Bubblesort
"In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. The individual items in a matrix are called its elements or entries. An example of a matrix with 2 rows and 3 columns is: "
Taken from: http://en.wikipedia.org/wiki/Matrix_(mathematics)
"A matrix which is formed by turning all the rows of a given matrix into columns and vice-versa. The transpose of matrix A is written AT."
1 image says more than N words
Taken from: http://www.mathwords.com/t/transpose_of_a_matrix.htm
"In object-oriented programming, an indexer allows instances of a particular class or struct to be indexed just like arrays"
"Indexers are implemented through the not get and set accessors for the operator. They are similar to properties, but differ by not being static, and the fact that indexers' accessors take parameters."
Taken from: http://en.wikipedia.org/wiki/Indexer_(programming)
"In computer programming, array slicing is an operation that extracts certain elements from an array and packages them as another array, possibly with different number of indices (or dimensions) and different index ranges."
Taken from: http://en.wikipedia.org/wiki/Array_slicing