MS你错的还不是少 for (int st = 0; st < al.Length; st++) { al[st] = "待定"; }
al 是string类型的数组,每一个元素都为string 你用int 来循环遍历,肯定错了。 string[] al = new string[10]; List<int> ls = new List<int>(); for(int i=0;i<10;i++) { ls.Add(i); } foreach (int st in ls) { al[st] = st.ToString()+"AAA"; } 可以参考
st 就是值了,不是索引。 为什么不用forstring[] al = new string[10]; for (int st = 0;i<al.Length;++st ) al[st] = "待定";
foreach的话 你可以理解为只读。 具体的看下MSDN吧
用for赋值 foreach是读取数据 foreach(string str in al) { // }
你是string型的数组,怎么用int型去遍历? 楼主,你还根本不懂foreach的用法吧
al 是个数组,st 是 int 类型,当然会出错,你要把数组的值都赋为“待定”吗? 如果是这样的话,可以用 一个for循环实现 string[] al = new string[10]; for (int i = 0; i < al.Length; i++) { al[i] = "待定"; }
我觉得楼主可能是想这样 string[] al = new string[10]; foreach (string st in al) { st = "待定"; } 但是st是foreach的一个迭代变量,不能给他赋值。所以还是用1楼的办法吧,不要用foreach了
string[] al = new string[10]; for (int st=0;st<al.Length ;st++) { al[st] = "OK"+st.ToString(); } 你上面的例子会提示,类型不对的.int 型怎么能和string 型 用in关键字呢.改用for语句吧.
{
al[st] = "待定";
}
al 是string类型的数组,每一个元素都为string
你用int 来循环遍历,肯定错了。
string[] al = new string[10];
List<int> ls = new List<int>();
for(int i=0;i<10;i++)
{
ls.Add(i);
}
foreach (int st in ls)
{
al[st] = st.ToString()+"AAA";
}
可以参考
为什么不用forstring[] al = new string[10];
for (int st = 0;i<al.Length;++st )
al[st] = "待定";
你可以理解为只读。
具体的看下MSDN吧
foreach是读取数据
foreach(string str in al)
{
//
}
楼主,你还根本不懂foreach的用法吧
如果是这样的话,可以用 一个for循环实现
string[] al = new string[10];
for (int i = 0; i < al.Length; i++)
{
al[i] = "待定";
}
foreach (string st in al)
{
st = "待定";
}
但是st是foreach的一个迭代变量,不能给他赋值。所以还是用1楼的办法吧,不要用foreach了
for (int st=0;st<al.Length ;st++)
{
al[st] = "OK"+st.ToString();
}
你上面的例子会提示,类型不对的.int 型怎么能和string 型 用in关键字呢.改用for语句吧.