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
8
votes
2 answers

Jagged Arrays in Data | Text To Columns

What I have Let's take an example of this code which works. Sub Sample() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet1") ws.Columns(1).TextToColumns _ Destination:=Range("A1"), _ DataType:=xlFixedWidth, _ …
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
7
votes
3 answers

Why does PowerShell flatten arrays automatically?

I've written some pwsh code "a:b;c:d;e:f".Split(";") | ForEach-Object { $_.Split(":") } # => @(a, b, c, d, e, f) but I want this // in javascript "a:b;c:d;e:f".split(";").map(str => str.split(":")) [ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e', 'f' ] ] a…
Kk Shinkai
  • 316
  • 2
  • 10
7
votes
13 answers

C#/C++: How to visualize muli-dimensional arrays

For example: A two-dimensional array can be visualized like a brick-wall with square bricks, where every brick represents a coordinate in our array. A 3-dimensional array can in the same way be visualized as a box, or cube. But, here is the tricky…
Marcus Hansson
  • 816
  • 2
  • 8
  • 17
7
votes
2 answers

Local reference to inner array in swift

I came from C# world, and used to arrays being reference types. As I understand, in swift arrays are value types, but they try to play as reference ones. I don't actually know how to ask what I need (I think this is the case when I need to know…
Lanorkin
  • 7,310
  • 2
  • 42
  • 60
7
votes
2 answers

Avoiding Agnostic Jagged Array Flattening in Powershell

I'm running into an interesting problem in Powershell, and haven't been able to find a solution to it. When I google (and find things like this post), nothing quite as involved as what I'm trying to do comes up, so I thought I'd post the question…
mlhDev
  • 2,235
  • 1
  • 22
  • 43
7
votes
3 answers

Matlab: 2d-array, rows different lengths

In Matlab, I want to create a two-dimensional array. However, I cannot create a matrix, because the rows are all different lengths. I am new to Matlab, and I would normally do this in C++ by creating an array of pointers, with each pointer pointing…
Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
7
votes
2 answers

C# jagged array type declaration in reverse

I just had one of these "What the..." moments. Is the following intended and is there some obscure reasoning behind the "non-natural" declaration of arrays in C#? int[,][] i; // declares an 2D - array where each element is an int[] ! // you have to…
Imi
  • 1,579
  • 1
  • 12
  • 22
6
votes
1 answer

How to assign to a jagged array?

I am writing a short program which will eventually play connect four. Here it is so far, pastebin There is one part which isn't working. I have a jagged array declared on line 16: char[][] board = Enumerable.Repeat(Enumerable.Repeat('-',…
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
6
votes
2 answers

How to invert the values of a logical jagged array in c#?

Good afternoon, I have a c# jagged array with true and false values (or 0's and 1's) and I want to reverse the values like: 1 1 1 1 0 1 1 0 1 1 1 1 1 0 0 1 to became: 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1 0 Is there an easy way to do it not looping it?…
Nauzet
  • 661
  • 8
  • 20
6
votes
0 answers

How does C# 3.0 jagged array performance optimization vs. rectangular arrays work?

In the Apress book "Illustrated C# 2008", pg. 343 notes: "One-dimensional arrays have specific instructions in the CIL that allow them to be optimized for performance. Rectangular arrays do not have these instructions...Because of this, it…
Scott Davies
  • 3,665
  • 6
  • 31
  • 39
6
votes
3 answers

Dynamically storing matrix values in C#

I'm trying to build an application in c#.net Here I am having two single-dimensional arrays of same size For example, I have matrices M and N like the below structure: M[0] M[1] M[2] M[3] M[4] N[0] N[1] N[2] N[3] N[4] Here I have…
Vivek Dragon
  • 2,218
  • 4
  • 27
  • 48
5
votes
1 answer

F# - Convert Jagged Array to Array2D

@scrwtp provides a very useful function (toJagged): let toJagged<'a> (arr: 'a[,]) : 'a [][] = [| for x in 0 .. Array2D.length1 arr - 1 do yield [| for y in 0 .. Array2D.length2 arr - 1 -> arr.[x, y] |] |] that converts from a 2D…
matekus
  • 778
  • 3
  • 14
5
votes
1 answer

Is Julia's Vector{Vector{T}} stored contiguously in memory?

To motivate my question, consider the case when dealing with jagged arrays (for simplicity's sake) of element type Int in Julia. There are two ways to store them: As Vector{Vector{Int}} As Vector{Union{Vector{Int}, Int}} (especially, if one expects…
aberdysh
  • 1,634
  • 2
  • 13
  • 34
5
votes
2 answers

How to find unique values in jagged array

I would like to know how I can count the number of unique values in a jagged array. My domain object contains a string property that has space delimitered values. class MyObject { string MyProperty; //e.g = "v1 v2 v3" } Given a list of…
David
  • 15,150
  • 15
  • 61
  • 83
5
votes
2 answers

column operations on jagged arrays (sum, average, other functions) with Linq

Assuming we have a jagged array with equal length item arrays (i.e. ignore catching out of ranges): int[][] jaggedArray = new int[][] { new int[] {1, 3, 5}, new int[] {0, 2, 4}, new int[] {11,22,6} }; what is the most elegant way to…
Keith
  • 375
  • 2
  • 7
1 2
3
38 39