2016/06/27

C#集合相关

.count  //获取集合元素数量

.contains()//确定某元素是否存在于集合中

 .sort()//升序排序

.Reverse()//翻转整个集合,搭配.Sort()可以实现降序排序

集合特点:

不需要指定元素数量,数组需要指定元素数量。集合只有一维,数组可以多维。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Specialized;

namespace 集合
{
class Program
{
static void Main(string[] args)
{
//Console.Write("请输入人数:");
//int a = int.Parse(Console.ReadLine());
//ArrayList x = new ArrayList();
//int sum = 0;
//for (int i = 0; i < a; i++)
//{
// Console.Write("请输入第{0}个人的成绩:",i+1);
// x.Add(int.Parse(Console.ReadLine()));
// sum+=(int)x[i];
//}
//x.Sort();
//Console.WriteLine("平均分为:"+sum/a);
//Console.WriteLine("最高分为:"+(int)x[a-1]);
//Console.WriteLine("最低分为:"+(int)x[0]);
//Console.ReadLine();

//Console.Write("请输入人数:");
//int a = int.Parse(Console.ReadLine());
//ArrayList jh = new ArrayList();


//for (int i = 0; i < a; i++)
//{

// Console.WriteLine("请输入第{0}个人的名字:",i+1);
// jh.Add(Console.ReadLine());
// Console.WriteLine("请输入第{0}个人的年龄:", i + 1);
// jh.Add(int.Parse((Console.ReadLine())));

//}
//for (int i =1 ; i < a*2; i=i+2)
//{
// for (int j = i; j < a*2 - 2; j=j+2)
// {
// if (int.Parse(jh[i].ToString())<int.Parse(jh[j+2].ToString()))
// {
// int huan = 0;
// huan = (int)jh[i];
// jh[i] = (int)jh[j+2];
// jh[j+2] = huan;
// string o = "";
// o = (string)jh[i - 1];
// jh[i - 1] = jh[j+1];
// jh[j + 1] = o;

// }


// }

//}
//int z=0;
//int v=1;
//for (int i = 0; i < a; i++)
//{
// Console.WriteLine("姓名:"+jh[z]);
// Console.WriteLine("年龄:"+jh[v]);
// z = z + 2;
// v = v + 2;
//}
//Console.WriteLine("年龄最大为"+jh[0]);
//Console.ReadLine();

//foreach(object aa in jh)//遍历集合
//{
//}


//遍历数组
//int[] array = new int[] { 2, 3, 4, 5, 6, 7, 8, 9, 2 };
//foreach (int aa in array)
//{
// Console.WriteLine(aa);
//}
//Console.ReadLine();

}
}
}

//今日编写如上

原文地址:https://www.cnblogs.com/blueteasama/p/5620204.html