I have a class piece where I define each piece shape.
myShape.Add(new piece
{
Height = 3,
Width = 2,
Name = "3x2 L TopRight",
Size = 4,
Shape = new int[][] {
new int[] { 1, 0 },
new int[] { 1, 0 },
new int[] { 1, 1 }
}
});
But I create those shape by hand, now I reading the pieces in real time, so I create something like
List<int[]> virtualRow = new List<int[]>();
virtualRow.Add(new int[] { 1, 0 });
virtualRow.Add(new int[] { 1, 0 });
virtualRow.Add(new int[] { 1, 1 });
So how can I create Shape using virtualRow ?
I try something like
Shape = new int[][] { virtualRow.ToArray() }
But say
Cannot implicitly convert type 'int[][]' to 'int[]'
virtualRow.ToArray()
is already an array of array of int
values. You don't need to create a new array of array of ints and add this to it.
All you need is:
Shape = virtualRow.ToArray(),
virtualRow is a List
of integer arrays, so to get an array of integer arrays you simply write:
Shape = virtualRow.ToArray();
...the return type of List.ToArray() being T[] as required.
Your code is in error because it attempts to add an int[][]
to Shape
instead of creating Shape
as an int[][]
.
You want to do the following:
Shape = virtualRow.ToArray();
Since virtualRow is already a list of arrays. The ToArray
function creates an int[][]
object for your virtualRow
, and all you need to do is store it to shape. What you were trying to do was create a matrix, within which was the result of the ToArray
function. This way you are just storing the result of the function which gives you what you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.