Jagged array
In computer science, a jagged array, also known as a ragged array or irregular array is an array 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.
Jagged array can be implemented with Iliffe vector data structure in languages such as Java, PHP, Python, Ruby, C#.NET, Visual Basic.NET, Perl, JavaScript, Objective-C, Swift, and Atlas Autocode.
Examples
In C# and Java jagged arrays can be created with the following code:int c;
c = new int; // creates 2 rows
c = new int; // 5 columns for row 0
c = new int; // create 3 columns for row 1
In C and C++, a jagged array can be created using the following code:
int jagged_row0 = ;
int jagged_row1 = ;
int *jagged = ;
In C/C++, jagged arrays can also be created with an array of pointers:
int *jagged;
jagged = malloc;
jagged = malloc;
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:
type :: Jagged_type
integer, allocatable :: row
end type Jagged_type
type :: Jagged
Jagged%row =
Jagged%row =
Jagged%row =
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] for i in range]
- Produces: , , , , , ,
- Produces: , , , , , , , , , , , , , , , , , , , ,