集合 - Array && ArrayList

集合:

ArrayList 属于非泛型集合; ArrayList 是一个数组集合. 

它属于动态改变长度的数组集合,不需要我们管理.

 ArrayList使用效率会低一些   因为有一个数据转换的过程

1、List的基础、常用方法:

声明: 
1、List<T> l = new List<T>();  
T为列表中元素类型,现在以string类型作为例子

比如:List<string> mList = new List<string>();

2、List<T> testList =new List<T> (IEnumerable<T> collection);

以一个集合作为参数创建List

比如:
string[] temArr = { "A", "B, "C", "D, "E", "F", "J", "K" };
List<string> testList = new List<string>(temArr);

ArrayList 比 List 多一步装箱和拆箱的操作;所以 List 执行速率快得多

装箱和拆箱
  装就是把普通数据类型转成object对象类型
  拆就是把装好的object拆成装箱之前的真实类型(强制转换)
  占用内存,消耗内存太大


ArrayList的例子:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;                                   //特性命名空间
using System.Threading;

namespace 集合之ArrayList
{
	class Person
	{
		public string name;
		public string age;

		public Person(string name){
			this.name = name;
		}
	}
	
	class MainClass
	{
		
		[Obsolete("old method",true)]
		/// <summary>
		/// 非泛型集合添加元素演示
		/// </summary>
		public void Test1(){
			ArrayList al = new ArrayList ();
			//演示添加非对象元素:
			al.Add ("刘德华");
			al.Add ("张学友");
			al.Add (1);
//遍历方式1
//			for (int i = 0; i < al.Count; i++) {
//				Console.WriteLine (al[i]);
//			}
		
//遍历方式2
//			foreach (var item in al) {
//				Console.WriteLine (item);
//			}

			Console.WriteLine ("---");
		}


		/// <summary>
		/// 泛型集合添加元素演示
		/// </summary>
		public void Test2(){
			//<T> 这个T是指占位符,不代表任何意义
			List<string> list = new List<string> ();
			list.Add ("张学友");
			list.Add ("刘德华");
//			list.Add (1);

		}


		/// <summary>
		/// 演示非泛型集合添加10000000个元素所耗费的时间
		/// </summary>
		public void Test4(){
			ArrayList al = new ArrayList ();
			Stopwatch sw = new Stopwatch ();

			sw.Start ();
			for (int i = 0; i < 10000000; i++) {
				al.Add (i);
			}
			sw.Stop ();
//			Console.WriteLine ("非泛型集合ArrayList添加100万元素所耗费的时间:"+sw.Elapsed);

			Console.WriteLine ("1");
		}


		/// <summary>
		/// 演示泛型集合添加10000000个元素所耗费的时间
		/// </summary>
		public void Test5(){
			List<int> al = new List<int> ();
			Stopwatch sw = new Stopwatch ();

			sw.Start ();
			for (int i = 0; i < 10000000; i++) {
				al.Add (i);
			}
			sw.Stop ();
//			Console.WriteLine ("       泛型集合List添加100万元素所耗费的时间:"+sw.Elapsed);
			Console.WriteLine ("2");

		}

		/// <summary>
		/// 非泛型集合排序/倒排
		/// </summary>
		public void Test6(){
//			ArrayList al = new ArrayList ();
//			int[] arr = { 1, 2, 3, 4, 5, 6 };
//			al.AddRange (arr);
//			//倒排
			//			al.Reverse();
//			//排序
//			al.Sort();
			Console.WriteLine (al.Count);
//			foreach (var item in al) {
//				Console.WriteLine (item);
//			}
//			List<int> li = new List<int> ();

		}

		/// <summary>
		/// 研究arraylist的capacity
		/// </summary>
		public void Test7(){
			ArrayList al = new ArrayList (3);
			Console.WriteLine (al.Capacity);
			al.Add (1);
			al.Add (2);
			al.Add (3);
			al.Add (4);
			Console.WriteLine (al.Capacity);
		}



		public static void Main (string[] args)
		{
//			MainClass mc = new MainClass ();
//			new MainClass().Test1();

//			new MainClass ().Test4 ();
//			new MainClass ().Test5 ();

//			Thread th = new Thread (mc.Test4);
//			th.Start ();
//
//			Thread th2 = new Thread (mc.Test5);
//			th2.Start ();

//			new MainClass ().Test7 ();


			//练习1:设计一个学生系统,可以录入学生相关信息,并且能通过学号反向输出学生的
			//全部信息
			//练习2:根据上题,增加可以增加或删除学生功能



		}
	}
}

List的例子:

using System;
using System.Collections;
using System.Collections.Generic;
namespace 练习
{

	class Student {

		public int stu_Id;
		public string stu_Name;
		public float stu_Score;

		public Student (int stu_Id,string stu_Name,float stu_Score) {
			this.stu_Id = stu_Id;
			this.stu_Name = stu_Name;
			this.stu_Score = stu_Score;
		}
	}

	class MainClass
	{
		public static void Main (string[] args)
		{

			Console.WriteLine ("请输入输入需要增加的学生的个数");
			List<Student> ls = new List<Student> ();
			int a = int.Parse (Console.ReadLine ());
			for (int i = 0; i < a; i++) {
				Console.WriteLine ("请按顺序输入 学号、姓名、得分");

				int stu_Id = int.Parse (Console.ReadLine ());
				string stu_Name = Console.ReadLine ();
				float stu_Score = float.Parse (Console.ReadLine ());
				ls.Add (new Student (stu_Id, stu_Name, stu_Score));
			}

			Console.WriteLine ("请输入需要查询的学生的学号");
			int cheakID = int.Parse(Console.ReadLine());
			foreach (Student item in ls) {
				if (cheakID == item.stu_Id) {
					Console.WriteLine ("{0}号同学;他的姓名是:{1};他的得分是{2}",item.stu_Id,item.stu_Name,item.stu_Score);
				}
			}

			Console.WriteLine ("输入需要添加的学生的学号、姓名、得分");
			int stu_Id1= int.Parse (Console.ReadLine ());
			string stu_Name1 = Console.ReadLine ();
			float stu_Score1 = float.Parse (Console.ReadLine ());

			ls.Add (new Student (stu_Id1, stu_Name1, stu_Score1));

			Console.WriteLine ("此时表单中有");
			foreach (Student item in ls) {
				Console.WriteLine ("学号{0};姓名{1};得分{2}",item.stu_Id,item.stu_Name,item.stu_Score);
			}

			Console.WriteLine ("输入需要删除的学生的学号");
			int stu_Id2 = int.Parse (Console.ReadLine ());
			for (int i = 0; i < ls.Count; i++) {
				if (stu_Id2 == ls[i].stu_Id) {
					ls.Remove (ls [i]);
				}
			}

			Console.WriteLine ("此时表单中有");
			foreach (Student item in ls) {
				Console.WriteLine ("学号{0};姓名{1};得分{2}",item.stu_Id,item.stu_Name,item.stu_Score);
			}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值