DQbJA%{Uzyz{_qUpUVyzyy{jyz{DUyqz{i8pUwiy^zSywiyS^5aS}'gw2?czyqz{Uybzyqzbyqzy{UUDQ!JAiy%{yqzyqzwiyyqz{Uy,`|iywiywiyiy3{Uy,`|iywiywiyiy3{'#'#UUaAabCfEsDBCDeXWBC| G5799964VC~WB-~CWB~~YP5656WBWBP665656F5RZ

Jagged arrays.` Data comes in various shapes. Sometimes the shape is uneven. With a 2D array, we might waste a great amount of memory.`But with jagged arrays,` we can store (efficiently) many rows of varying lengths. Any type of data, reference or value, can be used.`First example.` Let us start. This code is a short (but complete) program you can run in a console project. It creates a jagged array of int elements. `It sets values in the array. The program then prints out the result at the end.`Int `int`Important: `Jagged arrays have different performance characteristics. Indexing jagged arrays is fast. Allocating them is somewhat slow.`Notes, above example.` This example declares a jagged array. The word jagged doesn't exist in the C# language. This means that you don't need to use this word in your code. `Info: `The program initializes some values in the jagged array. There are lots of square brackets.`Syntax: `This is different from a 2D array, which uses commas instead of pure brackets.`Notes, continued.` Indexes in the array are assigned to new int arrays. The earlier statement allocates only the list of empty references. You have to make your own arrays to put in it. `We see the array initializer syntax here, which is less verbose than some other ways.`Initialize Array `initialize-array`Notes, loops.` You will want to examine each item in the jagged array. We must call Length first on the array of references, and then again on each inner array. `Syntax: `It is important to know that the pairs of brackets indicate "jagged," and the comma in the brackets means "2D".`You can allocate different arrays for each row in jagged arrays separately. This means your program must use 1D arrays in parts.`2D arrays.` We determine how to choose between jagged arrays and 2D arrays. First ask the question: will every row in my collection have the same number of elements? `If so, you can consider 2D arrays, but often you have varying numbers of elements.`2D Array `2d`Performance: `Jagged arrays are faster and have different syntax. They are faster because they use the "newarr", vector IL calls internally.`Zero index: `The boost in performance with jagged arrays is because they are optimized for starting at 0 indexes.`IL: newarr `il`IL Disassembler `il-disassembler`Methods.` You can use the type of the jagged array in method signatures. They will be passed as references. This eliminates most copying. Only the reference is copied on each function call. `For local variables only, you can use the var implicit type syntax. This makes programs easier to read.`Var `var`Benchmark.` The .NET Framework has optimizations for single-dimension arrays within a jagged array. The 2D array cannot take advantage of them. `Thus: `Jagged have substantial optimizations in the intermediate language level. This can be exploited to speed up programs.`Jagged vs. 2D Array `jagged-2d-array-memory`A jagged little summary.` Jagged arrays are fast and easy to use once you learn the syntax. Prefer them to 2D arrays when performance is key. Be careful of excess memory usage. `Arrays `array

?@B @@@@@ { ?@?{B ??@Declare local jagged @ with 3 rows. ??B@[][]B jagged@BnewB @[3][];B ??@C@ a @@@the jagged @,@assign it. ??Bjagged[0]@BnewB @[2]; ??jagged[0][0]@1; ??jagged[0][1]@2;B ??@Set second row, initialized@zero. ??Bjagged[1]@BnewB @[1];B ??@Set third row, @@ initializer. ??Bjagged[2]@@B@B[3] { 3, 4, 5 };B ??@Pr@out all @s@the jagged @. ??B@(@i@0; i < jagged.L@; i++) ??{ ???@[] inner@@jagged[i]; ???@(@a@0; a < inner@.L@; a++) ???{ ????@.@(inner@[a]@B" "B); ???} ???@); ??} ?} } B 1 2 0 3 4 5BM@ that is reflected @MSIL test: C#B @ @@CompareIL() { ?B@[,]B twoD@@@[1, 1];B @1 x 1 @ ?BtwoD[0, 0]@1@?B@[][]B jag@@@[1][]@?jag[0]@@@[1]; ?jag[0][0]@1;B @1 x 1 jagged @ B}B2D @ code benchmarked: C#B @@; @ @@ { ?const @_max@100000; ?@?{B ??@Set up data. ??Bvar a1@@@[100, 100]; ??var a2@@@[100][]; ??@(@i@0; i < 100; i++) ??{ ???a2[i]@@@[100]; ??}B ??@Version 1: access 2D @. ??Bvar s1@@.@New(); ??B@B (@i@0; i < _max; i++) ??{ ???@(@a@0; a < 100; a++) ???{ ????@(@x@0; x < 100; x++) ????{ ?????@c@a1[a, x]; ????} ???} ??} ??s1@;B ??@Version 2: access jagged @. ??Bvar s2@@.@New(); ??B@B (@i@0; i < _max; i++) ??{ ???@(@a@0; a < 100; a++) ???{ ????@(@x@0; x < 100; x++) ????{ ?????@c@a2[a][x]; ????} ???} ??} ??s2@;B ??@Results. ??B@s1.@); ??@s2.@); ?} } BResultsB 2D @ access:? B3712.2539 msB Jagged @ access: B2090.3271 msB

!]jagged arrays