习题
练习1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace practise1
{
class Program
{
static void Main(string[] args)
{
int[] MyArray = new int[10];
int Sum = 0;
for(int i=0;i<10;i++)
{
MyArray[i] = i; //为数组赋值
}
for(int i=0;i<10;i++)
{
if(MyArray[i]%2==0)//取偶数
{
Sum += MyArray[i];//求和
}
}
Console.WriteLine(Sum);
}
}
}
练习2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace practise2
{
class Program
{
static void Main(string[] args)
{
int[] x = new int[5] { 4, 2, 1, 6, 5 };
int[] y = new int[5];
foreach (int i in x)
Console.Write(i);//输出x
Console.WriteLine("\n");
Array.Reverse(x);//颠倒x的顺序
y = (int[])x.Clone();//将颠倒之後的数据复制到y中
foreach (int i in y)
Console.Write(i);//输出y
}
}
}
练习3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;//添加命名空间以使用ArryList
namespace practise3
{
class Program
{
static void Main(string[] args)
{
ArrayList MyArrylist = new ArrayList(4);//创建ArryList对象
string Result = null;//为以后输出结果做准备
//利用add对象为其添加元素
MyArrylist.Add("Yuki");
MyArrylist.Add("17721528");
MyArrylist.Add("Hablee");
foreach (string element in MyArrylist)
Console.WriteLine(element + " ");
Console.WriteLine();
//使用Contains查找元素并输出结果
Console.WriteLine(MyArrylist.Contains("Yuki"));
}
}
}
练习4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace practise4
{
public class students
{
public string Name;
public string Id;
public char Sex;
public double Grades;
}
class Program
{
static void Main(string[] args)
{
students stus1 = new students();
stus1.Name = "Hablee";
stus1.Id = "17721528";
stus1.Sex = 'M';
stus1.Grades = 90.5;
students stus2 = new students();
stus2.Name = "Yuki";
stus2.Id = "17721567";
stus2.Sex = 'F';
stus2.Grades = 97.5;
List<students> Students = new List<students>();//使用List<T>进行存储
Students.Add(stus1);
Students.Add(stus2);//增加操作
Console.WriteLine($"增加後: ");
foreach (students stu in Students)
Console.WriteLine($"{stu.Name}\t{stu.Id}\t{stu.Sex}\t{stu.Grades}");
//遍历操作
Console.WriteLine();
Students.Remove(stus1);//删除操作
Console.WriteLine($"删除後: ");
foreach (students stu in Students)
Console.WriteLine($"{stu.Name}\t{stu.Id}\t{stu.Sex}\t{stu.Grades}");
Console.WriteLine();
Console.WriteLine(Students.Contains(stus1));//访问操作
}
}
}
练习5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace practise4
{
public class Students
{
public string Name;
public string Id;
public char Sex;
public double Grades;
}
class Program
{
static void Main(string[] args)
{
Dictionary<string, Students> students = new Dictionary<string, Students>();
//实例化一个Dictionary为students,使用Dictionary<k,v>进行存储
//增加学生信息
Students stu1 = new Students();//实例化一个学生
stu1.Name = "Hablee";
stu1.Id = "17721528";
stu1.Sex = 'M';
stu1.Grades = 90.5;
students.Add("Hablee", stu1);
Students stu2 = new Students();//实例化另外一个学生
stu2.Name = "Yuki";
stu2.Id = "17721567";
stu2.Sex = 'F';
stu2.Grades = 97.5;
students.Add("Yuki", stu2);
Console.WriteLine($"增加後: ");
foreach (KeyValuePair<string, Students> valuePair in students)
Console.WriteLine($"{valuePair.Key}的信息\t{valuePair.Value.Id}\t{valuePair.Value.Name}\t" +
$"{valuePair.Value.Sex}\t{valuePair.Value.Grades}");
//遍历操作
Console.WriteLine();
students.Remove("Hablee");//删除操作
Console.WriteLine($"删除後: ");
foreach (KeyValuePair<string, Students> valuePair in students)
Console.WriteLine($"{valuePair.Key}的信息\t{valuePair.Value.Id}\t{valuePair.Value.Name}\t" +
$"{valuePair.Value.Sex}\t{valuePair.Value.Grades}");
//遍历操作
Console.WriteLine();
Console.WriteLine(students.ContainsKey("Yuki"));
Console.WriteLine(students.ContainsKey("Hablee"));//访问操作,判断是否还在
}
}
}
以上代码均在visual studio 2017中编译通过并运行成功
题外话:感觉Dictionary<k,v>比List<T>要好用那么一丢丢,可能本人水平不够,无法用到List<T>的精髓。