字典值的总和 [英] Sum of dictionary value
问题描述
字典<字符串, int [,] > 矩阵= new 字典<字符串, int [,]
// 获取数组中字典的值
public < span class =code-keyword> object [] Tab_Mat(Dictionary< string, int [,] > a)
{
object [] ruleArray = new object [a.Count];
int num = 0 ;
foreach (KeyValuePair< string, int [,] > 规则 in a)
ruleArray [num ++] = rule.Value;
return ruleArray;
}
// 获取字典值的总和
public int [,] sum_global_Mat(Dictionary< string, int [,] > config)
{
var a = Tab_Mat(config);
var newArray = Array.ConvertAll(a,item = > ( INT 跨度> [,])项);
int [,] m = new int [newArray [ 0 ]。GetLength( 0 ),newArray [ 0 ]。GetLength( 1 )];
for ( int i = 0 ; i< a.Length; i ++)
{
for ( int h = 0 ; h < m.GetLength( 0 ); h ++)
for ( int j = 0 ; j < m.GetLength( 1 ); j ++)
m [h,j] =( int )a [i] +( int )a [i + 1];
}
return m;
}
>();
我的尝试:
Hello evry one我是C#中的一个重要人物,
我试着做一本字典值的总和,知道这个值是Matrix,但它给我以下错误:
'System.InvalidCastException'
< blockquote>
Quote:'System.InvalidCastException'
帮助了一下,但知道哪一行会让我们的工作更轻松......
猜测,就是这一行:
m [h,j] =(int)a [i] +(int)a [i +1];
,这意味着 a
不包含整数。首先看看Tab_Mat究竟返回了什么,然后使用调试器找出 a [i]
和中的确切内容a [i + 1]
问题发生时。
我们不能为您执行此操作:我们在运行代码时无法访问您的代码!
Dictionary<string, int[,]> Matrices = new Dictionary<string, int[,]
//Get the values of dictionary in an array
public object[] Tab_Mat(Dictionary<string, int[,]> a)
{
object[] ruleArray = new object[a.Count];
int num = 0;
foreach (KeyValuePair<string, int[,]> rule in a)
ruleArray[num++] = rule.Value;
return ruleArray;
}
//Get the sum of the values of the dictionary
public int[,] sum_global_Mat(Dictionary<string, int[,]> config)
{
var a = Tab_Mat(config);
var newArray = Array.ConvertAll(a, item => (int[,])item);
int[,] m = new int[newArray[0].GetLength(0), newArray[0].GetLength(1)];
for ( int i=0;i<a.Length;i++)
{
for (int h = 0; h < m.GetLength(0); h++)
for (int j = 0; j < m.GetLength(1); j++)
m[h,j] =(int)a[i]+(int)a[i+1] ;
}
return m;
}
>();
What I have tried:
Hello evry one i am a biginner in C#,
I tried to do the sum of values of a dictionary ,knowing that this values are Matrix,but it give me the following error :
'System.InvalidCastException'
Quote:'System.InvalidCastException'
Helps a bit, but knowing which line would make our job easier...
At a guess, it's this line:m[h,j] =(int)a[i]+(int)a[i+1] ;and it implies that
a
does not contain integers. So start by looking at what exactly Tab_Mat does return, then use the debugger to find out exactly what is ina[i]
anda[i + 1]
when the problem occurs.
We can't do that for you: we don't have any access to your code while it's running!
这篇关于字典值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!