System.Collections.Generic的各容器类的用法

演示System.Collections.Generic的各容器类的用法.

包括:Dictionary,KeyValuePair,SortedDic tionary,SortedList,HashSet,SortedSet,List,Queue,Stack等

 

  1 System.Collections.Generic.Dictionary<>;       //键/值对集合
  2 System.Collections.Generic.KeyValuePair<>;     //键/值对结构, 作为 Dictionary<> 的一个元素存在
  3 System.Collections.Generic.SortedDictionary<>; //相当于 Key 能自动排序 Dictionary<>
  4 System.Collections.Generic.SortedList<>;       //和 SortedDictionary<> 功能相似, 但内部算法不同, 其 Keys、Values 可通过索引访问
  5 
  6 System.Collections.Generic.HashSet<>;   //无序、无重复的元素集合
  7 System.Collections.Generic.SortedSet<>; //相当于能自动排序的 HashSet<>
  8 System.Collections.Generic.List<>;      //相当于泛型的 ArrayList, 元素可重复、可排序、可插入、可索引访问
  9 
 10 System.Collections.Generic.Queue<>; //队列, 先进先出
 11 System.Collections.Generic.Stack<>; //堆栈, 后进先出
 12 
 13 System.Collections.Generic.LinkedList<>;     //双向链表
 14 System.Collections.Generic.LinkedListNode<>; //LinkedList<> 的节点
 15 
 16 System.Collections.Generic.SynchronizedCollection<>;         //线程安全的集合
 17 System.Collections.Generic.SynchronizedReadOnlyCollection<>; //线程安全的只读集合
 18 System.Collections.Generic.SynchronizedKeyedCollection<>;    //线程安全的键/值集合
 19 
 20 Dictionary<>、KeyValuePair<>:
 21 protected void Button1_Click(object sender, EventArgs e)
 22 {
 23     Dictionary<string, int> dict = new Dictionary<string, int>();
 24     dict.Add("K1", 123);
 25     dict["K2"] = 456;
 26     dict.Add("K3", 789);
 27 
 28     string str = "";
 29     foreach (KeyValuePair<string, int> k in dict)
 30     {
 31         str += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-123; K2-456; K3-789; 
 32     }
 33     TextBox1.Text = str;
 34 }
 35 
 36 SortedDictionary<>:
 37 protected void Button1_Click(object sender, EventArgs e)
 38 {
 39     SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
 40     dict.Add("K3", 333);
 41     dict["K1"] = 111;
 42     dict.Add("K2", 222);
 43 
 44     SortedDictionary<string, int>.KeyCollection ks = dict.Keys;
 45     SortedDictionary<string, int>.ValueCollection vs = dict.Values;
 46 
 47     string s1, s2, s3;
 48     s1 = s2 = s3 = "";
 49 
 50     foreach (KeyValuePair<string, int> k in dict)
 51     {
 52         s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;
 53     }
 54 
 55     foreach (string s in ks) { s2 += s + "; "; }          //K1; K2; K3;
 56     foreach (int n in vs) { s3 += n.ToString() + "; "; }  //111; 222; 333; 
 57 
 58     TextBox1.Text = s1 + "\\n" + s2 + "\\n" + s3;
 59 }
 60 
 61 SortedList<>:
 62 protected void Button1_Click(object sender, EventArgs e)
 63 {
 64     SortedList<string, int> dict = new SortedList<string, int>();
 65     dict.Add("K3", 333);
 66     dict["K1"] = 111;
 67     dict.Add("K2", 222);
 68 
 69     string s1, s2, s3;
 70     s1 = s2 = s3 = "";
 71 
 72     foreach (KeyValuePair<string, int> k in dict)
 73     {
 74         s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;
 75     }
 76 
 77     s2 = dict.Keys[0];              //K1
 78     s3 = dict.Values[0].ToString(); //111
 79 
 80     TextBox1.Text = s1 + "\\n" + s2 + "\\n" + s3;
 81 }
 82 
 83 HashSet<>、SortedSet<>:
 84 protected void Button1_Click(object sender, EventArgs e)
 85 {
 86     HashSet<string> hs = new HashSet<string>();
 87     hs.Add("ccc");
 88     hs.Add("bbb");
 89     hs.Add("aaa");
 90 
 91     SortedSet<string> ss = new SortedSet<string>();
 92     ss.Add("ccc");
 93     ss.Add("bbb");
 94     ss.Add("aaa");
 95 
 96     string s1 = "", s2 = "";
 97 
 98     foreach (string s in hs) { s1 += s + " "; } //ccc bbb aaa 
 99     foreach (string s in ss) { s2 += s + " "; } //aaa bbb ccc 
100 
101     TextBox1.Text = s1 + "\\n" + s2;
102 }
103 
104 List<>:
105 protected void Button1_Click(object sender, EventArgs e)
106 {
107     List<int> list = new List<int>();
108     list.Add(11);
109     list.Add(22);
110     list.Insert(0, 33);
111 
112     string s1, s2 = "", s3, s4 = "";
113 
114     s1 = list[0].ToString(); //33
115     for (int i = 0; i < list.Count; i++) { s2 += list[i].ToString() + " "; } //33 11 22
116 
117     list.Sort();
118 
119     s3 = list[0].ToString(); //11
120     foreach (int n in list) { s4 += n.ToString() + " "; } //11 22 33 
121 
122     TextBox1.Text = s1 + "\\n" + s2 + "\\n" + s3 + "\\n" + s4;
123 }
124 
125 LinkedList<>、LinkedListNode<>:
126 protected void Button1_Click(object sender, EventArgs e)
127 {
128     LinkedList<string> list = new LinkedList<string>();
129     list.AddFirst("aaa");
130     list.AddLast("bbb");
131     list.AddFirst("ccc");
132     list.AddAfter(list.First, "ddd");
133     list.AddBefore(list.Last, "eee");
134 
135     string s1 = "", s2 = "", s3 = "", s4 = "", s5 = "";
136 
137     foreach (string s in list) { s1 += s + " "; } //ccc ddd aaa eee bbb 
138 
139     LinkedListNode<string> node = list.First;
140     s2 = node.Value.ToString();         //ccc
141     node = node.Next;
142     s3 = node.Value.ToString();         //ddd
143     node = list.Last.Previous.Previous;
144     s4 = node.Value.ToString();         //aaa
145 
146     list.Remove("eee");
147     list.RemoveFirst();
148     list.RemoveLast();
149 
150     node = list.First;
151     while (node != null)
152     {
153         s5 += node.Value.ToString() + " "; //ddd aaa 
154         node = node.Next;
155     }
156     TextBox1.Text = s1 + "\\n" + s2 + "\\n" + s3 + "\\n" + s4 + "\\n" + s5;
157 }

 

 

转载于:https://www.cnblogs.com/JerryTian/p/5148884.html

发布了164 篇原创文章 · 获赞 75 · 访问量 22万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

出错 使用泛型 型“System.Collections.Generic.IEnumerable<T>”要“1”个型实参

05-13

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using InDesign; using System.Diagnostics; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Create application instance Type type = Type.GetTypeFromProgID("InDesign.Application"); InDesign.Application application = (InDesign.Application)Activator.CreateInstance(type); // Set unit type application.ViewPreferences.HorizontalMeasurementUnits = idMeasurementUnits.idMillimeters; application.ViewPreferences.VerticalMeasurementUnits = idMeasurementUnits.idMillimeters; // Create new document application.Documents.Add(true, application.DocumentPresets.FirstItem()); // Get active document and change some settings Document document = application.ActiveDocument; document.DocumentPreferences.FacingPages = false; document.DocumentPreferences.PageWidth = 210; document.DocumentPreferences.PageHeight = 297; // Get first page (already created) and set margins Page page = (Page)document.Pages[1]; page.MarginPreferences.Top = 10; page.MarginPreferences.Bottom = 10; page.MarginPreferences.Left = 20; page.MarginPreferences.Right = 10; // Create rectangle and fit an image into it InDesign.Rectangle rectangle = page.Rectangles.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, page); rectangle.GeometricBounds = new[] { 20, 30, 120, 130 }; rectangle.Place(@"c:\temp\sample.png", false); rectangle.Fit(idFitOptions.idContentToFrame); // Create second page and set margins page = document.Pages.Add(idLocationOptions.idUnknown, document); page.MarginPreferences.Top = 10; page.MarginPreferences.Bottom = 10; page.MarginPreferences.Left = 20; page.MarginPreferences.Right = 10; // Create a text frame and add some text TextFrame textFrame = page.TextFrames.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, page); textFrame.GeometricBounds = new[] { 20, 30, 120, 130 }; textFrame.Contents = "line1\rline2\rline3"; IEnumerator paragraphs = textFrame.Paragraphs.GetEnumerator(); for (int j = 0; j < textFrame.Paragraphs.Count; j++) { paragraphs.MoveNext(); Paragraph paragraph = ((Paragraph)paragraphs.Current); paragraph.Justification = idJustification.idCenterAlign; paragraph.PointSize = 15 - j; paragraph.FontStyle = "Regular"; paragraph.AppliedFont = "Verdana"; } } } } 这个是用c#在indesign中新建文档等。大家帮忙看看怎么修改 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览