您的位置:首页 > 编程语言 > C#

S2 深入.NET平台和C#语言 第三章 课上练习

2017-02-12 14:17 585 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace 深入.NET平台第三章_课上
{
class Program
{

/*
* 自动扩容:
* 如果结合中一个元素都没有的话,Capacity为0,
* 如果集合中出现第一个元素,分配4个空间,
* 如果出现第5个元素的时候,可以容纳8个元素,
* 每次扩容会扩成上次的2倍
*
*
* 1.1那么,系统怎么知道何时扩容?在哪个方法中进行了扩容。
* 解析:Add方法扩容,容量不够的时候
*
*/

static void Main(string[] args)
{

int[] nums = new int[3];
ArrayList ss = new ArrayList();
ss.Add(1);
ss.Add(2);
ss.Add(2);
ss.Add(3);
ss.Add(4);

foreach (Object item in list)
{

Console.WriteLine(item);
}

Console.WriteLine(ss.Capacity + "   容量");  //  6
Console.WriteLine(ss.Count + " 真正存储元素个数");//5

ArrayList engineers = new ArrayList(){
new student(){  Name="王",Author="hh",Price=20   },
new student(){  Name="王高达",Author="苏苏",Price=40   },
new student(){  Name="王地方",Author="恩恩",Price=390   },
};
Console.WriteLine(engineers.Count.ToString());
// ForeachTest();
student student1 = new student("如果蜗牛有爱情", "丁墨", 50);
student student2 = new student("花千骨", "果儿", 60);
student student3 = new student("盗墓笔记", "南派三叔", 70);

list.Add(student1);
list.Add(student2);
list.Add(student3);
foreach (student item in list)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("===================修改===============");

foreach (student item in list)
{

if (item.Name.Equals("如果蜗牛有爱情"))
{
item.Name = "他来了,请闭眼";
break;
}
}
foreach (student item in list)
{
Console.WriteLine(item.Name);
}
//按索引删除
Console.WriteLine("===================================我是高贵的分界线================");
list.RemoveAt(0);
foreach (student item in list)
{
Console.WriteLine(item.Name);
}

//按对象删除
list.Remove("花千骨");
Console.WriteLine("===================================我是高贵的分界线================");

foreach (student item in list)
{
Console.WriteLine(item.Name);
}
//清空所有
Console.WriteLine("===================================我是高贵的分界线================");
list.Clear();
foreach (student item in list)
{

Console.WriteLine(item.Name);
}

//对象初始化器
student stu = new student() { Name = "苏琳琳" };
ArrayList l = new ArrayList()
{
stu
};

//双列集合
Hashtable table = new Hashtable();
table.Add("CBC", "建设银行");
table.Add("ABC", "中国农业银行");
table.Add("ICBC", "工商银行");
//方式一:遍历 根据key拿到 VaLue,Next key的 集合
foreach (string key in table.Keys)
{
Console.WriteLine("key是:"+key);
//value 如何通过key  拿到 value!
//索引器
object value = table[key];
Console.WriteLine("value是:"+value.ToString());
}
Console.WriteLine("=================================================");
//方式二:直接遍历value
foreach (string value in table.Values)
{
Console.WriteLine(value);
}
//03 key和value一次遍历一个项目===(key+value)
foreach (DictionaryEntry item in table)
{
Console.WriteLine(item.Key+"  "+item.Value);
}

Console.ReadLine();
}
static ArrayList list = new ArrayList();
public static void ForeachTest()
{
student student1 = new student("如果蜗牛有爱情", "丁墨", 50);
student student2 = new student("花千骨", "果儿", 60);
student student3 = new student("盗墓笔记", "南派三叔", 70);

list.Add(student1);
list.Add(student2);
list.Add(student3);
foreach(student item in list){
Console.WriteLine(item.Name);
}

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: