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
12
votes
3 answers

How to use LINQ on a multidimensional array to 'unwind' the array?

Consider the following array: int[,] numbers = new int[3, 2] { { 2, 1 }, { 3, 4 }, { 6, 5 } }; I would like to use LINQ to construct an IEnumerable with numbers 2, 1, 3, 4, 6, 5. What would be the best way to do so?
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
11
votes
4 answers

What is the definition of a "true" multidimensional array and what languages support them?

Most of the programming books I have ever read, have the following line: "X language does not support true multidimensional arrays, but you can simulate (approximate) them with arrays of arrays." Since most of my experience has been with C-based…
11
votes
1 answer

How to initialize a jagged array in JavaScript?

Is it possible to have a jagged array in JavaScript? Here is the format of the data I want to store in a jagged array: (key)(value1, value2, value3) Can I put this in a jagged array?
John Keats
  • 187
  • 2
  • 2
  • 9
11
votes
1 answer

What's the workaround for "ragged/jagged tensors" in PyTorch?

Tensorflow provides ragged tensors (https://www.tensorflow.org/guide/ragged_tensor). PyTorch however doesn't provide such a data structure. Is there a workaround to construct something similar in PyTorch? import numpy as np x = np.array([[0], [0,…
Jensun Ravichandran
  • 959
  • 1
  • 11
  • 28
11
votes
6 answers

Initialize a Jagged Array the LINQ Way

I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using the traditional loop: var myArr = new double[rowCount][]; for (int i = 0; i < rowCount; i++) { myArr[i] = new double[colCount]; } I thought maybe…
user132748
10
votes
0 answers

Issues with nullable reference types for arrays of arrays

Of course, C# supports unidimensional and multidimensional arrays, and supports arrays of arrays. Famously, if, in an array of arrays, the inner array type has a different rank (dimension count) than the outer array, then the C# language and the…
10
votes
6 answers

Array of Arrays

How do you create an array of arrays in C#? I have read about creating jagged arrays but I'm not sure if thats the best way of going about it. I was wanting to achieve something like this: string[] myArray = {string[] myArray2, string[]…
Bali C
  • 30,582
  • 35
  • 123
  • 152
10
votes
4 answers

Converting jagged array to 2D array C#

I'm trying to convert this function from Jagged Array to 2D array, and I'm not able to convert everything Original Function: public static double[][] InvertMatrix(double[][] A) { int n = A.Length; //e will represent each column in the…
WT86
  • 823
  • 5
  • 13
  • 34
9
votes
1 answer

Multidimensional arrays, nullable reference types and type conversion

With C# 8's nullable reference types, we can write (for reference types): T x = ...; T? y = x; However, I'm having trouble understanding the conversion rules for multidimensional and jagged arrays. string[][] a = new string[1][]; string?[]?[] b =…
Timo
  • 9,269
  • 2
  • 28
  • 58
9
votes
4 answers

A workaround for a big multidimensional array (Jagged Array) C#?

I'm trying to initialize an array in three dimension to load a voxel world. The total size of the map should be (2048/1024/2048). I tried to initialize an jagged array of "int" but I throw a memory exception. What is the size limit? Size of my…
Mehdi Bugnard
  • 3,889
  • 4
  • 45
  • 86
9
votes
2 answers

How do I create a jagged 2d array in Java?

Our homework assignment asks us to use a jagged array to store the values of a two dimensional boolean matrix. Is there a built in java class for the jagged array or am I going to have to manually create it with an Array of ArrayLists?
Lilluda 5
  • 1,111
  • 3
  • 18
  • 38
8
votes
2 answers

Fastest way to convert T[,] to T[][]?

So it turns out all arrays are not created equal. Multi-dimensional arrays can have non-zero lower bounds. See for example Excel PIA's Range.Value property object[,] rectData = myRange.Value; I need to convert these data into a jagged array. My…
dFlat
  • 819
  • 7
  • 19
8
votes
2 answers

How do I sort jagged array by row in C#?

I have 2D jagged array. And I want to sort it by any rows. I've searched and found code for sorting by columns private static void Sort(T[][] data, int col) { Comparer comparer = Comparer.Default; Array.Sort(data, (x,y) =>…
stereo
  • 81
  • 1
  • 4
8
votes
3 answers

C# Using Linq to get column from jagged array

How do I get elements of a column from a jagged array as a flat array using Linq ???? public class Matrix { private readonly T[][] _matrix; public Matrix(int rows, int cols) { _matrix = new T[rows][]; for (int r = 0;…
nixgadget
  • 6,983
  • 16
  • 70
  • 103
8
votes
1 answer

Manual indexing over jagged arrays?

I'm at a performance bottleneck in my program where I need to access elements from an array millions of times in a tight loop. I looked around and the general consensus seems to be that even though multidimensional arrays should be faster, their…
John Smith
  • 83
  • 4
1
2
3
38 39