using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_01集合初步
{
class Book
{
public string BookName { get; set; }
public string BookAuthor { get; set; }
public int BookPrice { get; set; }
public Book() { }
public Book(string bookname, string bookauthor, int bookprice)
{
this.BookName = bookname;
this.BookAuthor = bookauthor;
this.BookPrice = bookprice;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_01集合初步
{
class Program
{
static void Main(string[] args)
{
//ListBeginOneDemo();
ForeachTest();
foreach (Book item in list)
{
if (item.BookAuthor.Equals("韩寒"))
{
item.BookAuthor = "郭敬明";
break;
}
}
Console.WriteLine("===================修改===============");
foreach (Book item in list)
{
Console.WriteLine(item.BookAuthor);
}
}
private static void ListBeginOneDemo()
{
ArrayList list= new ArrayList();
list.Add("宋小宝");
list.Add("张靓颖");
list.Add("徐小倩");
foreach (string item in list)
{
Console.WriteLine(item);
}
for (int i = 0; i < list.Count; i++)
{
if (list[i].Equals("宋小宝"))
{
list[i] = "JustinBiber";
break;
}
}
Console.WriteLine("修改后==========");
foreach (string item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("删除=========分割线=========");
list.Remove("徐小倩");
foreach (string item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("根据索引删除=========分割线=========");
Console.WriteLine("张靓颖");
foreach (string item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("删除所有=========分割线=========");
list.Clear();
foreach (string item in list)
{
Console.WriteLine(item);
}
}
//使用foreach修改引用的值
static ArrayList list = new ArrayList();
private static void ForeachTest()
{
Book book1 = new Book("你是人间的四月天", "林微因", 50);
Book book2 = new Book("艰难的抉择", "卡莉·菲奥莉娜", 60);
Book book3 = new Book("三重门", "韩寒", 70);
list.Add(book1);
list.Add(book2);
list.Add(book3);
foreach (Book item in list)
{
Console.WriteLine(item.BookAuthor);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_02自动扩容
{
/*
* 自动扩容:
* 如果结合中一个元素都没有,Capacity为0,
* 如果集合中出现第一个元素,分配4个空间,
* 如果出现第5个元素的时候,可以容纳8个元素,
* 每次扩容会扩成上次的2倍
*/
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(1);
list.Add(2);
list.Add(2);
list.Add(2);
list.Add(3);
foreach (Object item in list)
{
Console.WriteLine(item);
}
Console.WriteLine(list.Capacity + " 容量"); // 6
Console.WriteLine(list.Count + " 真正存储元素个数");//5
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_03集合初始化器
{
class Book
{
public string Anthor { get; set; }
public string BookName { get; set; }
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_03集合初始化器
{
class Program
{
static void Main(string[] args)
{
//对象初始化器
Book book = new Book() { Anthor="蝎子木二口",BookName="邪恶的力量"};
ArrayList list = new ArrayList()
{
book
};
Console.WriteLine(list);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_04Dictionary双列集合
{
class Program
{
static void Main(string[] args)
{
Hashtable table = new Hashtable();
table.Add("CBC", "建设银行");
table.Add("ABC", "中国农业银行");
table.Add("ICBC", "工商银行");
//方式一:通过Key便利Value
foreach (string key in table.Keys)
{
Console.WriteLine("Key是"+key);
object values = table[key];
Console.WriteLine("Values是"+values);
}
//方式二:直接遍历value
foreach (string value in table.Values)
{
Console.WriteLine(value);
}
//方法三:key和Value一次遍历一个项目
foreach (DictionaryEntry item in table)
{
Console.WriteLine(item.Key + " " + item.Value);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_05_单列泛型集合DictionaryT
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
//添加
list.Add("baby");
list.Add("love");
list.Add("me");
foreach (string item in list)
{
Console.WriteLine(item);
}
//删除
Console.WriteLine("=====================");
list.Remove("me");
foreach (string item in list)
{
Console.WriteLine(item);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_06_泛型集合DictionaryTV
{
public class Book
{
public string BookId { get; set; }
public string BookName { get; set; }
public int Price { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _03_06_泛型集合DictionaryTV
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Book> div = new Dictionary<string, Book>();
//初始化器
Book book1 = new Book() { BookId = "001", BookName = "战争与和平", Price =56 };
Book book2 = new Book() { BookId = "002", BookName = "狂人日记", Price = 78 };
Book book3 = new Book() { BookId = "003", BookName = "百年孤独", Price = 98 };
//添加
div.Add(book1.BookId, book1);
div.Add(book2.BookId, book2);
div.Add(book3.BookId, book3);
//根据key值获得values
foreach (string item in div.Keys)
{
Console.WriteLine("key是" + item);
//获取value
Book book = div[item];
Console.WriteLine(book.BookName);
}
//直接获取value
foreach (Book item in div.Values)
{
Console.WriteLine("图书的名称" + item.BookName);
}
// key 和 value
foreach (KeyValuePair<string, Book> item in div)
{
Console.WriteLine(item.Key);
Console.WriteLine(item.Value.BookName);
}
}
}
}