i have a list of 2d array like this:
我有这样一个二维数组列表:
static void Main(string[] args) {
List<int[,]> kidsL = new List<int[,]>();
int[,] square1 = new int[8, 8];
int[,] square2 = new int[8, 8];
int[,] square3 = new int[8, 8];
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
square1[i, j] = 1;
square2[i, j] = 2;
square3[i, j] = 3;
}
kidsL.Add(square1);
kidsL.Add(square2);
kidsL.Add(square3);
Console.WriteLine();
Console.Read();
}
i want to determine sum of every array and find the maxamim/minimum one (in this case the maximum one is 192).
我想确定每个数组的和并找到maxamim/minimum 1(在本例中,最大值为192)。
is there an easy way to do this or am I just going to have to loop through the old fashioned way?
有一种简单的方法来做这个吗?还是我要用传统的方法来循环?
2 个解决方案
#1
3
Well, you can use the following code to get IEnumarable<int>
from int[,]
可以使用以下代码从int[,]获取IEnumarable
var enumarable = from int item in square2
select item;
Also, you can use a Cast<int>()
method in order to unwrap int[,]
to IEnumarable<int>
.
此外,还可以使用Cast
Then you can use Max()
and Min()
linq method.
然后可以使用Max()和Min() linq方法。
var min = kidsL.Min(x => (from int item in x select item).Sum());
var max = kidsL.Max(x => (from int item in x select item).Sum());
// or
var min = kidsL.Min(x => x.Cast<int>().Sum())
or
或
var Max = (from int[,] array in kidsL
select (from int item in array select item).Sum())
.Max();
Update
更新
from int[,] array in kidsL select (from int item in array select item).Sum()
query returns you an IEnumarable<int>
which contains sums. In order to have the index of max, you should cast IEnumarable to array or list using ToList
or ToArray()
.
从kidsL选择中的int[,]数组(从数组选择项中的int项). sum()查询返回包含和的IEnumarable
var sumList = (from int[,] array in kidsL
select(from int item in array select item).Sum())
.ToList();
var maxSum = sumList.Max();
var maxInd = sumList.IndexOf(maxSum);
sumList
is a list of ints and contains sums. So then you can use Max
method to get max sum and IndexOf
to get index of the max.
sumList是一个ints列表,包含和。然后你可以用Max方法得到最大值和指数来得到最大值的索引。
#2
2
Cast<int>
method will flatten array to IEnumerable<int>
which allows using LINQ.
Cast
var max = kidsL.Max(square => square.Cast<int>().Sum());
var min = kidsL.Min(square => square.Cast<int>().Sum());
You should also be aware of possible overflow which can happen if values and dimensions of the array would be large.
还应该注意,如果数组的值和维度较大,可能会发生溢出。
is there an easy way to do this or am I just going to have to loop through the old fashioned way?
有一种简单的方法来做这个吗?还是我要用传统的方法来循环?
Although the solution is concise it has the same efficiency as looping over every element of every array. But that's indeed is an easy way.
虽然解决方案很简洁,但它的效率与循环遍历每个数组的每个元素是一样的。但这确实是一个简单的方法。
#1
3
Well, you can use the following code to get IEnumarable<int>
from int[,]
可以使用以下代码从int[,]获取IEnumarable
var enumarable = from int item in square2
select item;
Also, you can use a Cast<int>()
method in order to unwrap int[,]
to IEnumarable<int>
.
此外,还可以使用Cast
Then you can use Max()
and Min()
linq method.
然后可以使用Max()和Min() linq方法。
var min = kidsL.Min(x => (from int item in x select item).Sum());
var max = kidsL.Max(x => (from int item in x select item).Sum());
// or
var min = kidsL.Min(x => x.Cast<int>().Sum())
or
或
var Max = (from int[,] array in kidsL
select (from int item in array select item).Sum())
.Max();
Update
更新
from int[,] array in kidsL select (from int item in array select item).Sum()
query returns you an IEnumarable<int>
which contains sums. In order to have the index of max, you should cast IEnumarable to array or list using ToList
or ToArray()
.
从kidsL选择中的int[,]数组(从数组选择项中的int项). sum()查询返回包含和的IEnumarable
var sumList = (from int[,] array in kidsL
select(from int item in array select item).Sum())
.ToList();
var maxSum = sumList.Max();
var maxInd = sumList.IndexOf(maxSum);
sumList
is a list of ints and contains sums. So then you can use Max
method to get max sum and IndexOf
to get index of the max.
sumList是一个ints列表,包含和。然后你可以用Max方法得到最大值和指数来得到最大值的索引。
#2
2
Cast<int>
method will flatten array to IEnumerable<int>
which allows using LINQ.
Cast
var max = kidsL.Max(square => square.Cast<int>().Sum());
var min = kidsL.Min(square => square.Cast<int>().Sum());
You should also be aware of possible overflow which can happen if values and dimensions of the array would be large.
还应该注意,如果数组的值和维度较大,可能会发生溢出。
is there an easy way to do this or am I just going to have to loop through the old fashioned way?
有一种简单的方法来做这个吗?还是我要用传统的方法来循环?
Although the solution is concise it has the same efficiency as looping over every element of every array. But that's indeed is an easy way.
虽然解决方案很简洁,但它的效率与循环遍历每个数组的每个元素是一样的。但这确实是一个简单的方法。