Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Examples  





2 See also  





3 References  














Jagged array






Български
Italiano
עברית
 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Memory layout of a jagged array.

Incomputer science, a jagged array, also known as a ragged array [1]orirregular 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.

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

InC# 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

InC 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);

InC++/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;
}

InFortran, 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]

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

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Jagged_array&oldid=1191959255"

    Category: 
    Arrays
    Hidden categories: 
    Articles with short description
    Short description is different from Wikidata
    Articles with example Python (programming language) code
     



    This page was last edited on 26 December 2023, at 20:39 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki