c# 数组查找统计问题,求助高手,急!谢……
问题如下:
二维数组为
0 1 1 1
0 1 1 1
1 0 1 0
第0,2,3列号存储在list中,在lv_TempCombine中将0,2,3列号连接组成字符串,再将lv_TempCombine字符串转成字符数组char[] charList,然后再二维数组中查找第0,2,3列中值全为1的行的数目count,并输出。
代码看起来很繁琐,但各个组成部分都在大程序中有用,不能增删。
代码:
string[,] shuzu = { { "0", "1", "1", "1" }, { "0", "1", "1", "1" }, { "1", "0", "1", "0" } };
List<char> list = new List<char>();
list.Add('0');
list.Add('2');
list.Add('3');
string lv_TempCombine = string.Empty;
int count = 0; int Flag = 0;
for (int j = 0; j < list.Count; j++)
{
lv_TempCombine += list[j].ToString();
}
char[] charList = lv_TempCombine.ToCharArray();
for (int i = 0; i < 3; i++)
{
for (int k = 0; k < charList.Length ; k++)
{
int kk;
kk = (int)charList[k];
if (shuzu[i, kk] == "1") { Flag = 0; }
else { Flag = 1; }
}
if (Flag == 1) { continue; }
if (Flag == 0) { count++; }
}
Console.Write(lv_TempCombine + " " + count);
Console.ReadLine();
错误:索引超出了数组界限。红色部分
恳请高手给以指点,感激……
------解决方案--------------------
string[,] shuzu = { { "0", "1", "1", "1" }, { "0", "1", "1", "1" }, { "1", "0", "1", "0" } };
List<string> list = new List<string>();
list.Add("0");
list.Add("2");
list.Add("3");
string[] str = list.ToArray();
for (int i = 0; i < 3; i++)
{
for (int k = 0; k < str.Length; k++)
{
int kk;
kk = int.Parse(str[k]);
if (shuzu[i, kk] == "1") { Flag = 0; }
else { Flag = 1; }
}
if (Flag == 1) { continue; }
if (Flag == 0) { count++; }
}