# Jagged array

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Jagged_array
> Markdown URL: https://mediated.wiki/source/Jagged_array.md
> Source: https://en.wikipedia.org/wiki/Jagged_array
> Source revision: 1341865964
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Multidimensional data structure}}
[[File:Jagged Array Representation.png|thumb|right|alt=numArr = [[1, 2, 3], [4, 5, 6, 7], [8, 9]]|Memory layout of a jagged array]]
In [computer science](/source/computer_science), a '''jagged  array''', also known as a '''ragged  array'''<ref>{{cite book |last=King |first=K. N. |author-link= |date= 2008|title=C Programming |url= |location= |publisher=W. W. Norton |page=301 |isbn=978-0-393-97950-3}}</ref> or '''irregular array'''<ref>{{Cite book |title=Handbook of Data Structures and Applications |publisher=CRC Press |year=2004}}</ref> is an [array](/source/array_data_structure) of arrays of which the member arrays can be of different lengths,<ref name="LibertyMacDonald2008">{{cite book|author1=Jesse Liberty|author2=Brian MacDonald|title=Learning C# 3.0|url=https://books.google.com/books?id=CgLgh5vQcPgC&pg=PA210|date=18 November 2008|publisher=O'Reilly Media, Inc.|isbn=978-0-596-55420-0|pages=210–}}</ref> producing rows of jagged edges when visualized as output. In contrast, [two-dimensional array](/source/two-dimensional_array)s are always rectangular<ref name="Box2002">{{cite book|author=Don Box|title=Essential .Net: The Common Language Runtime|url=https://books.google.com/books?id=Kl1DVZ8wTqcC&pg=PA138|year=2002|publisher=Addison-Wesley Professional|isbn=978-0-201-73411-9|pages=138}}</ref> so jagged arrays should not be confused with [multidimensional arrays](/source/Array_data_type), but the former is often used to emulate the latter.

Jagged  array can be implemented with [Iliffe vector](/source/Iliffe_vector) data structure in languages such as [Java](/source/Java_(programming_language)), [PHP](/source/PHP), [Python](/source/Python_(programming_language)) (multidimensional lists), [Ruby](/source/Ruby_(programming_language)), [C#.NET](/source/C_Sharp_(programming_language)), [Visual Basic.NET](/source/Visual_Basic.NET), [Perl](/source/Perl), [JavaScript](/source/JavaScript), [Objective-C](/source/Objective-C), [Swift](/source/Swift_(programming_language)), and [Atlas Autocode](/source/Atlas_Autocode).

==Examples==
In [C#](/source/C_Sharp_(programming_language)) and [Java](/source/Java_(programming_language))<ref>{{Cite news|url=https://www.geeksforgeeks.org/jagged-array-in-java/|title=Jagged Array in Java - GeeksforGeeks|date=2016-02-03|work=GeeksforGeeks|access-date=2018-08-13|language=en-US}}</ref> jagged arrays can be created with the following code:<ref name="DeitelDeitel2008">{{cite book|author1=Paul J. Deitel|author2=Harvey M. Deitel|title=C# 2008 for Programmers|url=https://books.google.com/books?id=sYzx_mZy0twC&pg=PA40|date=26 September 2008|publisher=Pearson Education|isbn=978-0-13-701188-9|pages=40}}</ref>
<syntaxhighlight 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
</syntaxhighlight>

In [C](/source/C_(programming_language)) and [C++](/source/C%2B%2B), a jagged array can be created (on the stack) using the following code:

<syntaxhighlight lang="c">
int jagged_row0[] = {0,1};
int jagged_row1[] = {1,2,3};
int *jagged[] = { jagged_row0, jagged_row1 };
</syntaxhighlight>

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

<syntaxhighlight lang="c">
int *jagged[5];

jagged[0] = malloc(sizeof(int) * 10);
jagged[1] = malloc(sizeof(int) * 3);
</syntaxhighlight>

In [C++/CLI](/source/C%2B%2B%2FCLI), jagged array can be created with the code:<ref>{{cite web|title=Jagged Arrays|url=http://www.functionx.com/cppcli/arrays/jagged.htm|website=FunctionX|accessdate=26 November 2014}}</ref>
<syntaxhighlight lang="cpp">
using namespace System;
int main()
{
    array<array<double> ^> ^ Arrayname = gcnew array <array<double> ^> (4); // array contains 4 
    //elements
    return 0;
}
</syntaxhighlight>

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

<syntaxhighlight 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]
</syntaxhighlight>

In [Python](/source/Python_(programming_language)), jagged arrays are not native but one can use [list comprehensions](/source/list_comprehensions) to create a multi-dimensional list which supports any dimensional matrix:<ref>{{cite web|title=Lists in Python Demystified|url=http://alvin.io/lists-in-python-demystified-part-2/|website=Alvin.io|accessdate=31 January 2016|archive-date=15 October 2016|archive-url=https://web.archive.org/web/20161015050710/http://alvin.io/lists-in-python-demystified-part-2/|url-status=dead}}</ref>

<syntaxhighlight 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: [[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]]
</syntaxhighlight>

==See also==
* [Variable-length array](/source/Variable-length_array)
* [Iliffe vector](/source/Iliffe_vector)

==References==
{{reflist}}

Category:Arrays
Category:Articles with example Python (programming language) code

---
Adapted from the Wikipedia article [Jagged array](https://en.wikipedia.org/wiki/Jagged_array) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Jagged_array?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
