• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#二维数组和集合

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

    本次课我主要学习了二维数组和集合的部分内容。

    在二维数组的部分中,我主要学习了二维数组的定义方法:int [,] array=new int [4,2];做了一个小练习:用二维数组打印自己的姓氏。

string[,] a = new string[11, 9]
{
{" "," "," "," ","■"," "," "," "," "},
{" "," "," "," ","■"," "," "," "," "},
{" ","■","■","■","■","■","■","■"," "},
{" "," "," ","■","■","■"," "," "," "},
{" ","■","■"," ","■"," ","■","■"," "},
{" "," ","■","■","■","■","■"," "," "},
{" "," "," "," "," ","■"," "," "," "},
{"■","■","■","■","■","■","■","■","■"},
{" "," "," "," ","■"," "," "," "," "},
{" "," "," ","■","■"," "," "," "," "},
{" "," "," "," ","■"," "," "," "," "},
};
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 9; j++)
{
Console.Write(a[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();

    然后,我学习了一个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 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();

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#使用X.509v.3证书的方法。发布时间:2022-07-14
下一篇:
C#可以直接调用的Win32API发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap