What Is a Two-Dimensional Array?
A two-dimensional array is a very common type of data structure and is used in one form or another by almost all computer programming languages. In such an array, data elements of the same type are arranged into a format that is typically depicted as a table with rows and columns. The specific techniques used to locate data elements within memory vary from language to language and case to case, but the most efficient varieties allow for simple mathematical computation to be used to locate the specific memory address of any given array element. Arrays are so common that many languages include the array as a basic data type.
Arrays are some of the most common data structures used by computer programmers. An array is defined as a series of data elements that can be uniquely identified by some number of indices. It is common practice to refer to an array as having a number of dimensions equal to the number of index items needed to locate a particular data element. In a one-dimensional array, which is essentially a list, each data element can be located by referencing its position in the list. A two-dimensional array uses two indices to identify each data element, and can be visualized as a table with rows and columns.
Every data element in a standard two-dimensional array consists of the same type of object. Array elements are most often simple variables, such as floats or integers. In principle, however, any type of information can be stored in an array, so long as each item is the same. A two-dimensional array is a natural choice for storing any data that would naturally be placed into a table, and this data type is very often used to do precisely that.
Ideally, an entire two-dimensional array can be located within a single continuous block of memory. This allows for very rapid access, as the specific memory address of each individual data element within a two-dimensional array can be computed mathematically, by using a formula based on the size needed for each data item. In practice, this is not always possible, and arrays may be stored in different portions of memory, a process that reduces the speed with which elements can be accessed.
The most basic variety of two-dimensional array has a fixed size and uses integer values for indices. Many languages allow only integers to be used for index values, although it is often possible to build custom data types to avoid this limitation if necessary. Other varieties of two-dimensional array are optimized for specific purposes, such as storing arrays of largely empty cells or enabling dynamic resizing.
Discuss this Article
Post your comments