📑 Table of Contents
numArr = [[1, 2, 3], [4, 5, 6, 7], [8, 9]]
Memory layout of a jagged array

In computer science, a jagged array, also known as a ragged array[1] or irregular array[2] is an array of arrays of which the member arrays can be of different lengths,[3] producing rows of jagged edges when visualized as output. In contrast, two-dimensional arrays are always rectangular[4] 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

edit

In C# and Java[5] jagged arrays can be created with the following code:[6]

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:

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:

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:[7]

using namespace System;
int main()
{
    array<array<double> ^> ^ Arrayname = gcnew array <array<double> ^> (4); // array contains 4 
    //elements
    return 0;
}

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 = [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:[8]

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

See also

edit

References

edit
  1. ^ King, K. N. (2008). C Programming. W. W. Norton. p. 301. ISBN 978-0-393-97950-3.
  2. ^ Handbook of Data Structures and Applications. CRC Press. 2004.
  3. ^ Jesse Liberty; Brian MacDonald (18 November 2008). Learning C# 3.0. O'Reilly Media, Inc. pp. 210–. ISBN 978-0-596-55420-0.
  4. ^ Don Box (2002). Essential .Net: The Common Language Runtime. Addison-Wesley Professional. p. 138. ISBN 978-0-201-73411-9.
  5. ^ "Jagged Array in Java - GeeksforGeeks". GeeksforGeeks. 2016-02-03. Retrieved 2018-08-13.
  6. ^ Paul J. Deitel; Harvey M. Deitel (26 September 2008). C# 2008 for Programmers. Pearson Education. p. 40. ISBN 978-0-13-701188-9.
  7. ^ "Jagged Arrays". FunctionX. Retrieved 26 November 2014.
  8. ^ "Lists in Python Demystified". Alvin.io. Archived from the original on 15 October 2016. Retrieved 31 January 2016.

📚 Artikel Terkait di Wikipedia

Array (disambiguation)

Bit array or bit vector Dynamic array, allocated at run time Jagged array, an array of member arrays which can be of different lengths Parallel array of

Array (data structure)

column j of an array A would be accessed by double indexing (A[i][j] in typical notation). This alternative structure allows jagged arrays, where each row

Comparison of C Sharp and Java

every dimension of the array, as it is the case for jagged arrays). However, since all array element access in a multidimensional array requires multiplication/shift

Array (data type)

This way of emulating multi-dimensional arrays allows the creation of jagged arrays, where each row may have a different size – or, in general, where the

Jagged Alliance 2

Jagged Alliance 2 is a 1999 tactical role-playing game developed by Sir-Tech Canada for Microsoft Windows and later ported to Linux by Tribsoft. It is

Comparison of Visual Basic and Visual Basic .NET

requires zero-based arrays for interoperability between .NET languages. Visual Basic .NET introduced the concept of a jagged array, whereby the rows can

Iliffe vector

used to implement jagged arrays. An Iliffe vector for an n-dimensional array (where n ≥ 2) consists of a vector (or 1-dimensional array) of pointers to

C Sharp syntax

numbers2 = new int[3, 3] { {2, 3, 2}, {1, 2, 6}, {2, 4, 5} }; See also Jagged array Classes are self-describing user-defined reference types. Essentially