
In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, a jagged array, also known as a ragged array, irregular array is an
array
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
of arrays of which the member arrays can be of different lengths,
producing rows of jagged edges when visualized as output. In contrast, two-dimensional arrays are always rectangular
so jagged arrays should not be confused with
multidimensional arrays, but the former is often used to emulate the latter.
Arrays of arrays in languages such as Java, PHP, Python (multidimensional lists), Ruby, C#.NET,
Visual Basic.NET, Perl, JavaScript, Objective-C, Swift, and Atlas Autocode are implemented as
Iliffe vector
In computer programming, an Iliffe vector, 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-dimension ...
s.
Examples
In
C# and
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
jagged arrays can be created with the following code:
int[][]c;
c = new int[2][]; // creates 2 rows
c = new int // 5 columns for row 0
c = new int[3]; // create 3 columns for row 1
In C (programming language), C and C++, a jagged array can be created (on the stack) using the following code:
int jagged_row0[] = ;
int jagged_row1[] = ;
int *jagged[] = ;
In C/C++, jagged arrays can also be created (on the heap) with an array of pointers:
int *jagged
jagged = malloc(sizeof(int) * 10);
jagged = malloc(sizeof(int) * 3);
In
C++/CLI, jagged array can be created with the code:
using namespace System;
int main()
In
Fortran, a jagged array can be created using derived types with allocatable component(s):
type :: Jagged_type
integer, allocatable :: row(:)
end type Jagged_type
type(Jagged_type) :: Jagged(3)
Jagged(1)%row = Jagged(2)%row = ,2Jagged(3)%row = ,2,3
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
In
Python, jagged arrays are not native but one can use
list comprehensions to create a multi-dimensional list which supports any dimensional matrix:
multi_list_3d = [for i in range(3)">[.html" ;"title="[">[for i in range(3)for i in range(3)">html"_;"title="[">[<_a>for_i_in_range(3).html" ;"title="[.html" ;"title="[">[for i in range(3)">[.html" ;"title="[">[for i in range(3)for i in range(3)# Produces: [], [], [, ], [], [, ], [], []
multi_list_5d = [for i in range(5)] for i in range(5)]
# Produces: [], [], [], [], [, ], [], [], [], [, ], [], [], [], [, ], [], [], [], [, ], [], [], [], []
See also
* Variable-length array
*
Iliffe vector
In computer programming, an Iliffe vector, 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-dimension ...
References
Arrays
Articles with example Python (programming language) code
{{Computer-science-stub