jagged-arrays

Initializing jagged arrays

故事扮演 提交于 2019-11-27 20:37:01
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 int[10]; } but I am looking for a more beautiful way for it. May be something like that: int[][][] count = new int[10][10][10]; int[][][] my3DArray = CreateJaggedArray<int[][][]>(1, 2, 3); using static T CreateJaggedArray<T>(params int[] lengths) { return (T)InitializeJaggedArray(typeof(T).GetElementType(), 0, lengths); } static object InitializeJaggedArray(Type type,

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

淺唱寂寞╮ 提交于 2019-11-27 13:53:42
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 jagged array, I would simply call d[0] and that would give me an array of {1, 2, 3, 4, 5} , is there a way I can achieve the same with a 2D array? No. You could of course write a wrapper class that represents a slice, and has an indexer internally - but nothing inbuilt. The other approach would be to write a method that makes a copy of a slice and

jagged arrays <-> multidimensional arrays conversion in ASP.NET

北战南征 提交于 2019-11-27 07:13:38
问题 I would like some help to create the following convertions: A need to convert an 800*600 multidimensional array into a jagged array and then the same method in reverse (jagged array with the same data to the original multidimensional array) Is this possible? and any help about it? 回答1: I realize the question is a bit old but for flexability sake, I modfied the method a bit to calculate the array sizes within the method as opposed to having to pass them in: static object[][]

Initializing jagged arrays

爱⌒轻易说出口 提交于 2019-11-27 04:26:52
问题 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 int[10]; } but I am looking for a more beautiful way for it. May be something like that: int[][][] count = new int[10][10][10]; 回答1: int[][][] my3DArray = CreateJaggedArray<int[][][]>(1, 2, 3); using static T CreateJaggedArray<T>(params int[] lengths) { return (T

How to initialize a jagged array in JavaScript?

这一生的挚爱 提交于 2019-11-27 02:46:54
问题 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? 回答1: Yes, you can create that type of array using object or array literal grammar, or object/array methods. An array of arrays: // Using array literal grammar var arr = [[value1, value2, value3], [value1, value2]] // Creating and pushing to an array var arr = []; arr.push([value1, value2, value3]); An object of

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

南笙酒味 提交于 2019-11-27 02:46:54
问题 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? 回答1: How about: Enumerable .Range(0,numbers.GetUpperBound(0)+1) .SelectMany(x => Enumerable.Range(0,numbers.GetUpperBound(1)+1) .Select (y =>numbers[x,y] )); or to neaten up. var xLimit=Enumerable.Range(0,numbers.GetUpperBound(0)+1); var yLimit=Enumerable.Range(0,numbers

Converting from a jagged array to double pointer in C#

*爱你&永不变心* 提交于 2019-11-27 02:41:35
问题 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 doesn't seem to resolve the problem either. Is there any (preferably as efficient as possible) way to accomplish this in C#? I suspect the solution may not be very obvious at all, though I'm hoping for a straightforward one nonetheless. 回答1: A double[]

Multidimensional arrays in Java and C#

…衆ロ難τιáo~ 提交于 2019-11-26 20:39:48
问题 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 that the second method creates an array of arrays (slower access). However in Java, there is no such thing as [,], and I see multidimensional arrays declared like this: int[][] array3 = new int[32][32]; Since such syntax is illegal in C#, and Java has

How can I declare a two dimensional string array?

女生的网名这么多〃 提交于 2019-11-26 19:32:26
问题 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#? 回答1: string[,] Tablero = new string[3,3]; You can also instantiate it in the same line with array initializer syntax as follows: string[,] Tablero = new string[3, 3] {{"a","b","c"}, {"d","e","f"}, {"g","h","i"} }; 回答2: You probably want this: string[,] Tablero = new string[3,3]; This will create you a matrix-like array where all rows have the same length. The

What is a jagged array?

跟風遠走 提交于 2019-11-26 18:57:38
What is a jagged array (in c#)? Any examples and when should one use it.... A jagged array is an array of arrays. string[][] arrays = new string[5][]; That's a collection of five different string arrays, each could be a different length (they could also be the same length, but the point is there is no guarantee that they are). arrays[0] = new string[5]; arrays[1] = new string[100]; ... This is different from a 2D array where it is rectangular, meaning each row has the same number of columns. string[,] array = new string[3,5]; A jagged array is the same in any language, but it's where you have