Jagged array

Last updated
Memory layout of a jagged array. Jagged Array Representation.png
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.

Contents

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 vectors.

Examples

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

int[][]c;c=newint[2][];// creates 2 rowsc[0]=newint[5];// 5 columns for row 0c[1]=newint[3];// create 3 columns for row 1

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

intjagged_row0[]={0,1};intjagged_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]

usingnamespaceSystem;intmain(){array<array<double>^>^Arrayname=gcnewarray<array<double>^>(4);// array contains 4 //elementsreturn0;}

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

type::Jagged_typeinteger,allocatable::row(:)end type Jagged_typetype(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=[[[]foriinrange(3)]foriinrange(3)]# Produces: [[[], [], []], [[], [], []], [[], [], []]]multi_list_5d=[[[]foriinrange(5)]foriinrange(5)]# Produces: [[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]]

See also

Related Research Articles

In computer science, an array is a data structure consisting of a collection of elements, of same memory size, each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula. The simplest type of data structure is a linear array, also called one-dimensional array.

C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems, device drivers, and protocol stacks, but its use in application software has been decreasing. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.

Message Passing Interface (MPI) is a standardized and portable message-passing standard designed to function on parallel computing architectures. The MPI standard defines the syntax and semantics of library routines that are useful to a wide range of users writing portable message-passing programs in C, C++, and Fortran. There are several open-source MPI implementations, which fostered the development of a parallel software industry, and encouraged development of portable and scalable large-scale parallel applications.

In computer programming, specifically when using the imperative programming paradigm, an assertion is a predicate connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects.

In computer programming, the stride of an array is the number of locations in memory between beginnings of successive array elements, measured in bytes or in units of the size of the array's elements. The stride cannot be smaller than the element size but can be larger, indicating extra space between elements.

C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc, aligned_alloc and free.

<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

<span class="mw-page-title-main">Foreach loop</span> Control flow statement for traversing items in a collection

In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.

In computer programming, array slicing is an operation that extracts a subset of elements from an array and packages them as another array, possibly in a different dimension from the original.

<span class="mw-page-title-main">Row- and column-major order</span> Array representation in computer memory

In computing, row-major order and column-major order are methods for storing multidimensional arrays in linear storage such as random access memory.

In computer programming, an Iliffe vector, also known as a display, is a data structure used to implement multi-dimensional arrays.

The Boehm–Demers–Weiser garbage collector, often simply known as Boehm GC, is a conservative garbage collector for C and C++ developed by Hans Boehm, Alan Demers, and Mark Weiser.

sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.

The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time . In C, the VLA is said to have a variably modified type that depends on a value.

This comparison of programming languages (array) compares the features of array data structures or matrix processing for various computer programming languages.

In computer science, array is a data type that represents a collection of elements, each selected by one or more indices that can be computed at run time during program execution. Such a collection is usually called an array variable or array value. By analogy with the mathematical concepts vector and matrix, array types with one and two indices are often called vector type and matrix type, respectively. More generally, a multidimensional array type can be called a tensor type, by analogy with the physical concept, tensor.

In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function. It is semantically related to the concept of a coroutine and is often implemented using similar techniques, and is primarily intended to provide opportunities for the program to execute other code while waiting for a long-running, asynchronous task to complete, usually represented by promises or similar data structures. The feature is found in C# 5.0, C++20, Python 3.5, F#, Hack, Julia, Dart, Kotlin 1.1, Rust 1.39, Nim 0.9.4, JavaScript ES2017, Swift 5.5 and Zig, with some experimental work in extensions, beta versions, and particular implementations of Scala.

C struct data types may end with a flexible array member with no specified size:

References

  1. King, K. N. 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. Retrieved 31 January 2016.