一、二维数组
int [,] array = new int[5,3];//有五个一维数组,每一个一维数组有3个元素
/打印出来一个“王”这个字string[,] wang = new string[,]{
{" ","■","■","■","■","■"," "},
{" "," "," ","■"," "," "," "},
{" "," "," ","■"," "," "," "},
{" "," ","■","■","■"," "," "},
{" "," "," ","■"," "," "," "},
{" "," "," ","■"," "," "," "},
{"■","■","■","■","■","■","■"}
};
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write(wang[i,j]);
}
Console.WriteLine();
}
输入班级人数,输入每个人的语数英成绩,求语文的总分,数学的平均分,英语的最高分和最低分
Console.Write("请输入班级人数:"); int n = int.Parse(Console.ReadLine()); double sum1= 0; double sum2 = 0; double[,] a = new double[n, 3]; for (int i = 0; i < n; i++) { Console.Write("请输入第"+(i+1)+"个人的语文成绩:"); a[i, 0] = double.Parse(Console.ReadLine()); sum1 +=a[i,0]; Console.Write("请输入第" + (i + 1) + "个人的数学成绩:"); a[i, 1] = double.Parse(Console.ReadLine()); sum2 += a[i, 1]; Console.Write("请输入第" + (i + 1) + "个人的英语成绩:"); a[i, 2] = double.Parse(Console.ReadLine()); } for (int i = 0; i < n;i++ ) { for (int j = i; j < n-1;j++ ) { if (a[i,2]<a[j+1,2]) { double zhong=a[i,2]; a[i, 2] = a[j + 1, 2]; a[j + 1, 2] = zhong; } } } Console.WriteLine("语文总分为"+sum1+"分"); Console.WriteLine("数学平均分为"+sum2/n+"分"); Console.WriteLine("英语最高分为"+a[0,2]); Console.WriteLine("英语最低分为"+a[n-1,2]); Console.ReadLine();
二、多维数组
int[, ,] shuzu = new int[4, 3, 2];//有四个二维数组,每个二维数组里面有3个1维数组,每个1维数组里面有2个元素
string[, ,] gao = new string[3, 4, 1]
{
{
{"张三"},{"李四"},{"王五"},{"赵六"}
},
{
{"冯七"},{"王八"},{"张全蛋"},{"小沈阳"}
},
{
{"刘能"},{"赵四"},{"赵本山"},{"岳云鹏"}
}
};
split 分离
一次性接收姓名、年龄、工作单位
Console.Write("请输入您的姓名、年龄、工作单位(**-**-**):");
string quan = Console.ReadLine();
string[] array = quan.Split('-');
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
Console.ReadLine();
三、ArrayList集合
集合不同于数组,是一组可变数量的元素的组合,这些元素可能共享某些特征,需要以某种操作方式一起进行操作。一般来说,这些元素的类型是相同的。
集合与数组的区别:数组是连续的、同一类型数据的一块区域,而集合可以是不连续的、多种数据类型的。
ArrayList实现了IList、ICollection、IEnumerable接口;
ArrayList与Array的不同
(1)ArrayList是集合,而Array是数组;
(2)ArrayList是具体类,Array是抽象类;
(3)获取数组的元素数时使用Length属性,而获取ArrayList集合的元素数时使用Count属性;
(4)数组可以有多维,而ArrayList只能是一维。
ArrayList al = new ArrayList();//初始化
al.Add(5);
al.Add(3);
al.Add(6);
al.Add(4);
al.Add(1);
al.Add(2);
al.Add("2");
al.Add("hello");
al.Add(true);
al[3] = "你好";
al.Clear(); //移除所有元素
ArrayList al1 = new ArrayList();
al1 = (ArrayList)al.Clone();//克隆
Console.WriteLine(al1.Contains("2"));//判断是否包含
Console.WriteLine(al1.IndexOf(11));//获取第一次出现该元素的索引号
Console.WriteLine(al1.LastIndexOf(2));//获取最后一次出现该元素的索引号
al.Insert(2,"Hello");//插入某个索引位置某个元素
al.Remove("hello");//移除某个第一次出现的元素
al.RemoveAt(3);//移除某个索引位置的元素
al.Sort();//排序 ,升序
al.Reverse();//翻转整个集合
Console.WriteLine(al[2]);
Console.WriteLine(al.Count);
Console.ReadLine();
练习
//输入班级人数,输入每个人的姓名、语数英成绩进入集合
//求语文的总分,数学的平均分,
//英语的最高分的人的姓名及所有科目的分数(冒泡排序)
//0 姓名 4 8 12 //1 语文成绩5 9 13 //2 数学成绩6 10 14 //3 英语成绩7 11 15 Console.Write("请输入班级人数:"); int n = int.Parse(Console.ReadLine()); double yu = 0; double shu = 0; ArrayList al = new ArrayList(); for (int i = 0; i < n; i++) { Console.Write("请输入第{0}个人的姓名:",i+1); al.Add(Console.ReadLine()); Console.Write("请输入第{0}个人的语文成绩:",i+1); al.Add(double.Parse(Console.ReadLine())); yu += double.Parse(al[al.Count - 1].ToString()); Console.Write("请输入第{0}个人的数学成绩:", i + 1); al.Add(double.Parse(Console.ReadLine())); shu += double.Parse(al[al.Count - 1].ToString()); Console.Write("请输入第{0}个人的英语成绩:", i + 1); al.Add(double.Parse(Console.ReadLine())); } Console.WriteLine("所有信息输入完毕,请按回车键继续!"); Console.ReadLine(); Console.WriteLine("语文的总分是:"+yu); Console.WriteLine("数学的平均分是:"+(shu/n)); for (int i = 3; i < 4*n-1; i+=4) { for (int j = i; j < 4 * n - 4; j += 4) { if (double.Parse(al[i].ToString()) < double.Parse(al[j + 4].ToString())) { //换姓名 object zhong = al[i - 3]; al[i - 3] = al[j + 1]; al[j + 1] = zhong; //换语文 zhong = al[i - 2]; al[i - 2] = al[j + 2]; al[j + 2] = zhong; //换数学 zhong = al[i - 1]; al[i - 1] = al[j + 3]; al[j + 3] = zhong; //换英语 zhong = al[i]; al[i] = al[j + 4]; al[j + 4] = zhong; } } } Console.WriteLine("英语最高分的学生是{0},英语分数是{1},语文分数是{2},数学分数{3}",al[0],al[3],al[1],al[2]); Console.ReadLine();
集合不同于数组,是一组可变数量的元素的组合,这些元素可能共享某些特征,需要以某种操作方式一起进行操作。一般来说,这些元素的类型是相同的。
集合与数组的区别:数组是连续的、同一类型数据的一块区域,而集合可以是不连续的、多种数据类型的。
来源:https://www.cnblogs.com/zblc2016/p/5721725.html