aAaCfEsDBCDeXWBC| 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

OHUFYUUHDHQHbHJHAU{UOH%O{YUOOH{Declare local jagged HU with 3 rows.UOOYHz[][]Y jaggedHyYnewY Hz[3][];YUUOOH{CH_ a HqHUHpthe jagged HU,HVassign it.UOOYjagged[0]HyYnewY Hz[2];UOOjagged[0][0]Hy1;UOOjagged[0][1]Hy2;YUUOOH{Set second row, initializedHjzero.UOOYjagged[1]HyYnewY Hz[1];YUUOOH{Set third row, HDHU initializer.UOOYjagged[2]HyHqYHzY[3] { 3, 4, 5 };YUUOOH{PrHiout all H8sHpthe jagged HU.UOOYHw(HiiHy0; i < jagged.LH^; i++)UOO{UOOOHz[] innerHSHyjagged[i];UOOOHw(HiaHy0; a < innerHS.LH^; a++)UOOO{UOOOOH5.Ha(innerHS[a]H}YF FY);UOOO}UOOOH');UOO}UO}U}UUYUU1 2U0U3 4 5YMHg that is reflected HwMSIL test: C#YUUH2 H?HcCompareIL()U{UOYHz[,]Y twoDHyHqHz[1, 1];Y H{1 x 1 HUUUOYtwoD[0, 0]Hy1HbOYHz[][]Y jagHyHqHz[1][]HbOjag[0]HyHqHz[1];UOjag[0][0]Hy1;Y H{1 x 1 jagged HUUY}Y2D HU code benchmarked: C#YUUHDHQ;UH!UHJHAU{UOconst Hi_maxHy100000;UOH%O{YUOOH{Set up data.UOOYvar a1HyHqHz[100, 100];UOOvar a2HyHqHz[100][];UOOHw(HiiHy0; i < 100; i++)UOO{UOOOa2[i]HyHqHz[100];UOO}YUUOOH{Version 1: access 2D HU.UOOYvar s1HyH,.H`New();UOOYH|Y (HiiHy0; i < _max; i++)UOO{UOOOHw(HiaHy0; a < 100; a++)UOOO{UOOOOHw(HixHy0; x < 100; x++)UOOOO{UOOOOOHicHya1[a, x];UOOOO}UOOO}UOO}UOOs1H3;YUUOOH{Version 2: access jagged HU.UOOYvar s2HyH,.H`New();UOOYH|Y (HiiHy0; i < _max; i++)UOO{UOOOHw(HiaHy0; a < 100; a++)UOOO{UOOOOHw(HixHy0; x < 100; x++)UOOOO{UOOOOOHicHya2[a][x];UOOOO}UOOO}UOO}UOOs2H3;YUUOOH{Results.UOOYH's1.H#);UOOH's2.H#);UO}U}UUYResultsYUU2D HU access:O Y3712.2539 msYUJagged HU access: Y2090.3271 msY

!]jagged arrays