Jagged array

Multidimensional data structure


title: "Jagged array" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["arrays", "articles-with-example-python-(programming-language)-code"] description: "Multidimensional data structure" topic_path: "general/arrays" source: "https://en.wikipedia.org/wiki/Jagged_array" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Multidimensional data structure ::

::figure[src="https://upload.wikimedia.org/wikipedia/commons/2/2f/Jagged_Array_Representation.png" caption="Memory layout of a 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 (multidimensional lists), 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: ::code[lang=csharp] int[][] c; c = new int[2][]; // creates 2 rows c[0] = new int[5]; // 5 columns for row 0 c[1] = new int[3]; // create 3 columns for row 1 ::

In C and C++, a jagged array can be created (on the stack) using the following code:

::code[lang=c] int jagged_row0[] = {0,1}; int jagged_row1[] = {1,2,3}; int *jagged[] = { jagged_row0, jagged_row1 }; ::

In C/C++, jagged arrays can also be created (on the heap) with an array of pointers:

::code[lang=c] int *jagged[5];

jagged[0] = malloc(sizeof(int) * 10); jagged[1] = malloc(sizeof(int) * 3); ::

In C++/CLI, jagged array can be created with the code: ::code[lang=cpp] using namespace System; int main() { array<array ^> ^ Arrayname = gcnew array <array ^> (4); // array contains 4 //elements return 0; } ::

In Fortran, a jagged array can be created using derived types with allocatable component(s):

::code[lang=fortran] type :: Jagged_type integer, allocatable :: row(:) end type Jagged_type type(Jagged_type) :: Jagged(3) Jagged(1)%row = [1] Jagged(2)%row = [1,2] Jagged(3)%row = [1,2,3] ::

In Python, jagged arrays are not native but one can use list comprehensions to create a multi-dimensional list which supports any dimensional matrix:

::code[lang=python] multi_list_3d = [[[] 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: [[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]]

::

References

References

  1. King, K. N.. (2008). "C Programming". W. W. Norton.
  2. (2004). "Handbook of Data Structures and Applications". CRC Press.
  3. (18 November 2008). "Learning C# 3.0". "O'Reilly Media, Inc.".
  4. Don Box. (2002). "Essential .Net: The Common Language Runtime". Addison-Wesley Professional.
  5. (2016-02-03). "Jagged Array in Java - GeeksforGeeks". GeeksforGeeks.
  6. (26 September 2008). "C# 2008 for Programmers". Pearson Education.
  7. "Jagged Arrays".
  8. "Lists in Python Demystified".

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

arraysarticles-with-example-python-(programming-language)-code