Questions tagged [jagged-arrays]

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." Use for questions about such arrays. If the array is multidimensional with all elements in each dimension being equal(eg: all rows have the same length), use [multidimensional-arrays] instead.

In computer science, a jagged array, also known as a ragged array, is an array of arrays of which the member arrays can be of different sizes, producing rows of jagged edges when visualized as output. In contrast, C-styled arrays are always rectangular; 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 (come from here).

In C#, jagged arrays can be created with the following code:

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++/CLI, jagged array can be created with the code:

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

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

 multi_list_3d = [[[] for i in range(3)] for i in range(3)] # [[[], [], []], [[], [], []], [[], [], []]]
 multi_list_5d = [[[] for i in range(5)] for i in range(5)] # [[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []]]
582 questions
512
votes
12 answers

Differences between a multidimensional array "[,]" and an array of arrays "[][]" in C#?

What are the differences between multidimensional arrays double[,] and array of arrays double[][] in C#? If there is a difference? What is the best use for each one?
ecleel
  • 11,748
  • 15
  • 48
  • 48
181
votes
9 answers

Using lodash to compare jagged arrays (items existence without order)

I know I can do it using loops, but I'm trying to find an elegant way of doing this: I have two jagged arrays (array of arrays): var array1 = [['a', 'b'], ['b', 'c']]; var array2 = [['b', 'c'], ['a', 'b']]; I want to use lodash to confirm that the…
pQuestions123
  • 4,471
  • 6
  • 28
  • 59
87
votes
12 answers

How can I declare a two dimensional string array?

string[][] Tablero = new string[3][3]; I need to have a 3x3 array arrangement to save information to. How do I declare this in C#?
48
votes
7 answers

What is a jagged array?

What is a jagged array (in c#)? Any examples and when should one use it....
ACP
  • 34,682
  • 100
  • 231
  • 371
39
votes
4 answers

How to get a dimension (slice) from a multidimensional array

I'm trying to figure out how to get a single dimension from a multidimensional array (for the sake of argument, let's say it's 2D), I have a multidimensional array: double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } }; If it was a…
Kiril
  • 39,672
  • 31
  • 167
  • 226
36
votes
12 answers

Do jagged arrays exist in C/C++?

Is there such a thing as a jagged array in C or C++? When I compile this: int jagged[][] = { {0,1}, {1,2,3} }; I get this error: error: declaration of `jagged' as multidimensional array must have bounds for all dimensions except the first
unknown
  • 1,313
  • 3
  • 12
  • 7
29
votes
8 answers

Initializing jagged arrays

I want to create array 10 * 10 * 10 in C# like int[][][] (not int[,,]). I can write code: int[][][] count = new int[10][][]; for (int i = 0; i < 10; i++) { count[i] = new int[10][]; for (int j = 0; j < 10; j++) count[i][j] = new…
AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
24
votes
5 answers

How do I set up a "jagged array" in VBA?

I have a classroom full of kids, each of whom have to list their favorite toys for an assignment. Some kids only list 1 toy whereas others list more. How do I create a jagged array such that Kids(x)(y)...where x is the number of kids in my class,…
MrPatterns
  • 4,184
  • 27
  • 65
  • 85
22
votes
5 answers

How to convert list of arrays into a multidimensional array

I need to convert the following collection into double[,]: var ret = new List(); All the arrays in the list have the same length. The simplest approach, ret.ToArray(), produces double[][], which is not what I want. Of course, I can…
Arne Lund
  • 2,366
  • 3
  • 26
  • 39
20
votes
6 answers

Multi-Dimensional Arrays in C: are they jagged?

A simple question about the C programming language (ANSI-C): Are the multi-dimensional arrays in C jagged? I mean - are we talking about "array of arrays" (one array of pointers to other addresses in the memory) , or this is just "long…
Programmer
  • 750
  • 2
  • 9
  • 17
14
votes
3 answers

Converting from a jagged array to double pointer in C#

Simple question here: is there any way to convert from a jagged array to a double pointer? e.g. Convert a double[][] to double** This can't be done just by casting unfortunately (as it can in plain old C), unfortunately. Using a fixed statement…
Noldorin
  • 144,213
  • 56
  • 264
  • 302
14
votes
4 answers

Ragged and Jagged Arrays

What is the difference between ragged and jagged arrays? As per my research both have the same definitions, i.e. two-dimensional arrays with different column lengths.
Nitesh Verma
  • 1,795
  • 4
  • 27
  • 46
14
votes
4 answers

C++ 2 dimensional array with variable size rows

How can you create a 2D array,say, arr[][] with 5 rows and each row has a variable number of columns in it? possibly arr[5][] with 1st row arr[0][] with 4 columns 2nd row arr[1][] with 5 columns and so on? I wouldn't mind a STL vector solution but I…
user1484717
  • 877
  • 3
  • 9
  • 11
13
votes
6 answers

Multidimensional arrays in Java and C#

In C# there are 2 ways to create mutlidimensional arrays. int[,] array1 = new int[32,32]; int[][] array2 = new int[32][]; for(int i=0;i<32;i++) array2[i] = new int[32]; I know that the first method creates a 1-dimensional array internally, and…
Hannesh
  • 7,256
  • 7
  • 46
  • 80
13
votes
3 answers

Convert multidimensional array to jagged array in C#

I have a C# WCF webservice which is called by two VB 6 project. The target VB project is sending to the client VB project a multidimensional array. I want to convert the multidimensional array to a jagged array but i have no luck. How can i find…
sebastian.roibu
  • 2,579
  • 7
  • 37
  • 59
1
2 3
38 39