0728多维数组,ArrayList集合
一.多维数组
//一位数组
//int[] shu = new int[] { 1,2,3};
////二维数组
//int [,] array = new int[4,2];
////4,表示有四个一维数组
////2,表示每一个一维数组有2个元素
//int[,] shuzu = new int[,]
//{
//{1,2},
//{3,4},
//{5,6},
//{7,8}
//};
//for (int i = 0; i < 4; i++)
//{
// for (int j = 0; j < 2; j++)
// {
// Console.Write(shuzu[i,j]+"\t");
// }
//}
//Console.ReadLine();
//打印自己喜欢的图案
//string[,] a = new string[,]
//{
// {" ", " "," ","◆"," "," "," "},
// {" ", " ","◆"," ","◆"," "," "},
// {" ", "◆"," ","◆"," ","◆"," "},
// {"◆", " ","◆"," ","◆"," ","◆"},
// {" ", "◆"," ","◆"," ","◆"," "},
// {" ", " ","◆"," ","◆"," "," "},
// {" ", " "," ","■"," "," "," "},
//};
//for (int i = 0; i < 7; i++)
//{
// for (int j = 0; j < 7; j++)
// {
// Console.Write(a[i, j]);
// }
// Console.WriteLine();
//}
//Console.ReadLine();
//请输入班级人数,要求输入每个人的语数英进入数组
//求语文总分,数学平均分,英语最高分
//Console.Write("请输入班级人数:");
//int n = int.Parse(Console.ReadLine());
//double[,] fen = new double[n, 3];
//double yuzong = 0;
//double shuzong = 0;
//for (int i = 0; i < n; i++)
//{
// Console.Write("请输入第{0}个人的语文成绩:", i + 1);
// fen[i, 0] = double.Parse(Console.ReadLine());
// Console.Write("请输入第{0}个人的数学成绩:", i + 1);
// fen[i, 1] = double.Parse(Console.ReadLine());
// Console.Write("请输入第{0}个人的英语成绩:", i + 1);
// fen[i, 2] = double.Parse(Console.ReadLine());
//}
//for (int j = 0; j < n; j++)
//{
// yuzong += fen[j, 0];
//}
//Console.WriteLine("语文总分为:" + yuzong);
//for (int k = 0; k < n; k++)
//{
// shuzong += fen[k, 1];
//}
//Console.WriteLine("数学平均分为:" + shuzong / n);
//for (int z = 0; z < n-1;z++ )
//{
// if (fen[z, 2] > fen[z + 1, 2])
// {
// }
// else
// {
// fen[z, 2] = fen[z + 1, 2];
// fen[z, 1] = fen[z + 1, 2];
// fen[z, 0] = fen[z + 1, 0];
// }
// Console.WriteLine("英语最高分为:"+fen[z,2]);
//}
//Console.ReadLine();
//多维数组
//int[, ,] shuzu = new int[4, 3, 2];
////4,表示有四个二维数组
////3,表示每个二维数组里面有3个1维数组
////2,表示每个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 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();
//输入班级人数,将每个人的姓名放进集合,然后全部读取
//ArrayList name = new ArrayList();
//Console.Write("请输入班级人数:");
//int n = int.Parse(Console.ReadLine());
//for (int i = 0; i < n; i++)
//{
// Console.Write("请输入第{0}个人的姓名:",i+1);
// name.Add(Console.ReadLine());
//}
//Console.WriteLine("所有人员姓名输入完毕,请按回车键读取!");
//Console.ReadLine();
////foreach 遍历集合
//foreach (object aa in name)
//{
// Console.Write(aa+"\t");
//}
//Console.ReadLine();
//object a;
//int b = 4;
//a = b;
//double c = 3.14;
//a = c;
//bool d = true;
//a = d;
//string e = "Hello";
//a = e;
//DateTime dt = DateTime.Now;
//a = dt;
//输入班级人数,输入每个人的姓名,输入语数英成绩,进入集合
//求语文的总分,数学的平均分,英语的最高分的姓名及各科分数(冒泡排序)
第一种做法,在索引号上进行+4循环
//Console.Write("请输入班级人数:");
//int num = int.Parse(Console.ReadLine());
//ArrayList al = new ArrayList();
//double yuzong = 0;
//double shuzong = 0;
//for (int i = 0; i < num; i++)
//{
// Console.Write("请输入第{0}个人的姓名:", i + 1);
// al.Add(Console.ReadLine());
// Console.Write("请输入第{0}个人的语文成绩:", i + 1);
// al.Add(double.Parse(Console.ReadLine().ToString()));
// Console.Write("请输入第{0}个人的数学成绩:", i + 1);
// al.Add(double.Parse(Console.ReadLine().ToString()));
// Console.Write("请输入第{0}个人的英语成绩:", i + 1);
// al.Add(double.Parse(Console.ReadLine().ToString()));
// yuzong = double.Parse((al[1 + 4*i]).ToString()) + yuzong;
// shuzong = double.Parse((al[2 +4*i]).ToString()) + shuzong;
//}
//Console.WriteLine("语文总分为:" + yuzong);
//Console.WriteLine("数学平均分为:" + shuzong / num);
//求英语最高分,将每一次循环的所有元素全部调换
//for (int j = 0; j < num; j++)
//{
// for (int k = j; k < num - 1; k++)
// {
// if (double.Parse((al[3 + 4 * j]).ToString()) < (double.Parse((al[7 + 4 * k]).ToString())))
// {
// //换英语
// double zhong = double.Parse((al[3 + 4 * j]).ToString());
// al[3 + 4 * j] = al[7 + 4 * k];
// al[7 + 4 * k] = zhong;
// //换姓名
// object zhong1 = al[0 + 4 * j];
// al[0 + 4 * j] = al[4 + 4 * k];
// al[4 + 4 * k] = zhong1;
// //换语文
// double zhong2 = double.Parse((al[1 + 4 * j]).ToString());
// al[1 + 4 * j] = al[5 + 4 * k];
// al[5 + 4 * k] = zhong2;
// //换数学
// double zhong3 = double.Parse((al[2 + 4 * j]).ToString());
// al[2 + 4 * j] = al[6 + 4 * k];
// al[6 + 4 * k] = zhong3;
// }
// }
//}
//Console.WriteLine("{0}的英语成绩为最高分为{1},语文为{2},数学为{3}。", al[0], al[3], al[1], al[2]);
//Console.ReadLine();
第二种做法,在循环条件上+4
//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();