C# 二维数组和集合
本次课我主要学习了二维数组和集合的部分内容。
在二维数组的部分中,我主要学习了二维数组的定义方法: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 = ;
double shu = ;
ArrayList al = new ArrayList();
for (int i = ; i < n; i++)
{
Console.Write("请输入第{0}个人的姓名:",i+);
al.Add(Console.ReadLine());
Console.Write("请输入第{0}个人的语文成绩:",i+);
al.Add(double.Parse(Console.ReadLine()));
yu += double.Parse(al[al.Count - ].ToString());
Console.Write("请输入第{0}个人的数学成绩:", i + );
al.Add(double.Parse(Console.ReadLine()));
shu += double.Parse(al[al.Count - ].ToString());
Console.Write("请输入第{0}个人的英语成绩:", i + );
al.Add(double.Parse(Console.ReadLine()));
}
Console.WriteLine("所有信息输入完毕,请按回车键继续!");
Console.ReadLine();
Console.WriteLine("语文的总分是:"+yu);
Console.WriteLine("数学的平均分是:"+(shu/n)); for (int i = ; i < *n-; i+=)
{
for (int j = i; j < * n - ; j += )
{
if (double.Parse(al[i].ToString()) < double.Parse(al[j + ].ToString()))
{
//换姓名
object zhong = al[i - ];
al[i - ] = al[j + ];
al[j + ] = zhong;
//换语文
zhong = al[i - ];
al[i - ] = al[j + ];
al[j + ] = zhong;
//换数学
zhong = al[i - ];
al[i - ] = al[j + ];
al[j + ] = zhong;
//换英语
zhong = al[i];
al[i] = al[j + ];
al[j + ] = zhong;
}
}
}
Console.WriteLine("英语最高分的学生是{0},英语分数是{1},语文分数是{2},数学分数{3}",al[],al[],al[],al[]);
Console.ReadLine();
C# 二维数组和集合的更多相关文章
- C#(二维数组/集合)
一.二维数组int [,] array = new int[5,3];//有五个一维数组,每一个一维数组有3个元素 /打印出来一个“王”这个字string[,] wang = new string[, ...
- PHP 如何获取二维数组中某个key的集合(高性能查找)
分享下PHP 获取二维数组中某个key的集合的方法. 具体是这样的,如下一个二维数组,是从库中读取出来的. 代码: $user = array( 0 => array( 'id' => 1 ...
- PHP 获取二维数组中某个key的集合
本文为代码分享,也是在工作中看到一些“大牛”的代码,做做分享. 具体是这样的,如下一个二维数组,是从库中读取出来的. 代码清单: $user = array( 0 => array( 'id' ...
- 二维数组,锯齿数组和集合 C# 一维数组、二维数组(矩形数组)、交错数组(锯齿数组)的使用 C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)
二维数组,锯齿数组和集合 一.二维数组 二维数组:一维数组----豆角二维数组----表格 定义:1.一维数组:数据类型[] 数组变量名 = new 数据类型[数组长度];数据类型[] 数组变量名 = ...
- PHP使用array_filter查找二维数组中符合字段和字段值的数据集合
1.方法: /** * 获取符合字段和字段值的数组集合 * @param array $data 待过滤数组 * @param string $field 要查找的字段 * @param $value ...
- php获取二维数组中某一列的值集合
$result //二维数组$uid_list = array_column($result, 'uid');
- ***php解析JSON二维数组字符串(json_decode函数第二个参数True和False的区别)
客户端的请求体中的数据:[{"msg_id": 1, "msg_status": "HAS_READ" }, { "msg_id& ...
- Android 在资源文件(res/strings.xml)定义一维数组,间接定义二维数组
经常我们会在资源文件(res/strings.xml)定义字符串,一维数组,那定义二维数组?直接定义二维数组没找到,可以间接定义. 其实很简单,看过用过一次就可以记住了,一维数组估计大家经常用到,但是 ...
- 数组(Array),二维数组,三维数组
数组(Array):相同类型数据的集合就叫做数组. (一)定义数组的方法: A) type[] 变量名 = new type[数组中元素的个数] 例如: int[] a = new int[10] ; ...
随机推荐
- 李明杰的视频和李明杰的博客是学习OC的基础
http://www.cocoachina.com/bbs/read.php?tid=196664
- 微信小程序 textarea 简易解决方案
微信小程序中textarea没有bindchange事件,所以无法在输入时给变量赋值. 虽然可以使用bindblur事件,但是绑定bindblur事件,如果再点击按钮,则先执行完按钮事件后,再去执行b ...
- 初始go语言
一.创建第一个go语言程序:打印hello world! package main import "fmt" func main() { fmt.Println("Hel ...
- 第一篇,jos
关于jos环,使用递推公式简化问题和代码,关键在于找到正确的递推公式,可使用一个例子来寻找. (数学能力较差,只好打个表找规律了) 为方便取余运算,将编号1---n的下标表示为0--(n-1) ...
- Network: Why 1472B length of ICMP?
when ping, specifying the length of the packet by: ping localhost -l 32 Actually default is -l 32, s ...
- Python简记
1.字符换行: print('ab \ncd \nef')
- Android中帧动画的创建
帧动画,实质上就是快速播放多张连接效果的图片,现在一般可用于下拉刷新时候的headView 实现步骤: 1.首先应该准备一组连接效果的图片 2.在res>drawable目录下创建xml文件,将 ...
- Redis的windows安装
第一步:下载Windows下的Redis链接:http://web.kuaipan.cn/n/drive/files/179275650081763322 第二不:打开cmd窗口命令:定位的Redis ...
- mysql简单建表
MySQL 字段约束 null,not null,default,auto_increment [来源于] 没有规矩,不成方圆,任何事物都是如此.在MySQL中,每个字段定义都包含附加约束或者修饰符, ...
- 关于autoconf
1 the difference between AC_ARG_ENABLE and AC_ARG_WITH AC_ARG_ENABLE是enable一个feature,该feature所对应的源码包 ...