问题描述
- 学习C#时遇到一些 代码上的理解困难
-
string A = "11,9,23,14,36,48,100,99,123,111";
string B = "23,56,46,123,199,323,187,23";
string C = A + "," + B;
char[] buf = { ',' };
string[] datas = C.Split(buf);
string[] abc = new string[1000];
for (int i = 0; i < abc.Length; i++)
{
abc[i] = "";
}
for (int i = 0; i < datas.Length; i++)
{
string strA = datas[i];
int intA = Convert.ToInt32(datas[i]);
if (abc[intA].Length > 0)
{
abc[intA] = abc[intA] + strA + ",";
}
else
{
abc[intA] = strA + ",";
}
}
for (int i = 0; i < abc.Length; i++)
{
textBox3.Text += abc[i];
}
代码中的
if (abc[intA].Length > 0)
{
abc[intA] = abc[intA] + strA + ",";
}
else
{
abc[intA] = strA + ",";
}
这段代码是什么意思?
解决方案
string A = "11,9,23,14,36,48,100,99,123,111";//这里定义了一个字符串
string B = "23,56,46,123,199,323,187,23";//这里又定义了一个字符串
string C = A + "," + B;//这里字符串C的字符内容就是字符串A的加B的,也就是C=“11,9,23,14,36,48,100,99,123,111 , 23,56,46,123,199,323,187,23”;
char[] buf = { ',' };//这里定义了一个字符
string[] datas = C.Split(buf);//这行代码的意思是用字符buf将字符串C的内容分开也就得到一个字符串数组了:datas={"11","9","23","14","36","48","100","99","123","111","23","56","46","123","199","323","187","23"}
string[] abc = new string[1000];//这个是创建一个数组长度为1000的字符串数组,也就是是数组里能存1000个字符串;
for (int i = 0; i < abc.Length; i++)
{
abc[i] = "";//这里是给abc数组里每个元素赋值,下标是从零开始到999都赋空字符
}
for (int i = 0; i < datas.Length; i++)
{
string strA = datas[i];//循环将datas数组里的字符串赋给字符串strA
int intA = Convert.ToInt32(datas[i]);//将字符串数组datas里的字符串转换成32位整形数据
if (abc[intA].Length > 0)//如果字符串数组abc在角标为intA的字符串长度大于零
{
abc[intA] = abc[intA] + strA + ",";//字符串数组在intA角标位置的值就是该字符串数组在inA角标的字符串加上字符串strA的字符串值
}
else//如果字符串数组abc在角标为intA的字符串长度小于等于零
{
abc[intA] = strA + ",";//字符串数组abc在角标intA的值就是字符串strA的值加上逗号
}
}
for (int i = 0; i < abc.Length; i++)
{
textBox3.Text += abc[i];
}
代码中的
if (abc[intA].Length > 0)
{
abc[intA] = abc[intA] + strA + ",";
}
else
{
abc[intA] = strA + ",";
}
如果回答对您有帮助,请采纳