static void Main(string[] args) { string[] names ={"甲","乙","丙"}; for (int i = 0; i < names.Length; i++) { string name = names[i]; Console.WriteLine("{0}",name); } /* string[] str ={""}; str[0] = "a"; str[1] = "b"; //数组的长度是确定的,无法动态的增加长度 */ //3、将一个字符串数组输出为|分割的形式,比如“小月月|大月月|老月月” //方法一 /*string[] strspilt ={ "小月月","大月月","老月月"}; for (int i = 0; i < strspilt.Length-1; i++) { Console.Write("{0}|",strspilt[i]); } Console.Write("{0}",strspilt[strspilt.Length-1]); */ //方法二 string[] strspilt ={ "小月月", "大月月", "老月月" }; string s = ""; for (int i = 0; i < strspilt.Length-1; i++) { s += strspilt[i] + "|"; } s+= strspilt[strspilt.Length - 1]; Console.WriteLine(s); //空数组 string[] strspilt2 ={}; string s2 = ""; for (int i = 0; i < strspilt2.Length - 1; i++) { s2 += strspilt2[i] + "|"; } if (strspilt2.Length > 0) //如果没有这句话,字符串数组为0的话就会出问题 { s2 += strspilt2[strspilt2.Length - 1]; } Console.Write(s2); //将下列数组进行处理:如果正数加一,如果负数减一 int[] str3 ={ 12, 0, -3, 56 }; for (int i = 0; i < str3.Length; i++) { if (str3[i] > 0) { str3[i]++; } else if (str3[i] < 0) { str3[i]--; } } for (int i = 0; i < str3.Length; i++) { Console.WriteLine(str3[i]); } //Console.WriteLine(i); //变量作用域,注意 for中的i,出了for范围i就不起作用了 //练习5:有一个整数数组,请声明一个字符串数组,将整数数组中的每一个元素的值转换为字符串保存到字符串数组中 int[] i5 ={34,-23,0,45}; string[] str5 = new string[i5.Length]; for (int i = 0; i < i5.Length; i++) { str5[i] = Convert.ToString(i5[i]); } for (int i = 0; i < str5.Length; i++) { Console.WriteLine(str5[i]); } //6、将一个字符串数组的元素的顺序进行反转 string[] s6 ={ "a1", "a2", "a3", "a4" }; for (int i = 0; i < s6.Length/2; i++) { string temp = s6[i]; s6[i] = s6[s6.Length - 1-i]; s6[s6.Length - 1-i] = temp; } for (int i = 0; i < s6.Length; i++) { Console.WriteLine(s6[i]); } Console.ReadKey();