找到关于编程的34423 篇文章

在 C# 列表中插入元素到第二个位置

Samual Sam
更新于 2020年6月22日 14:08:04

498 次浏览

以下是我们的列表 −List val = new List (); // 字符串列表 val.Add("water"); val.Add("food"); val.Add("air"); 使用 Insert() 方法在列表中插入元素。同时设置插入位置。我们将新文本添加到第 1 个位置 −val.Insert(1, "shelter"); 以下是完整代码 −示例 在线演示using System; using System.Collections.Generic; public class Demo {    public static void Main() {       List val = new List ();       // 字符串列表       val.Add("water");       val.Add("food");       val.Add("air");       ... 阅读更多

C# 程序:从字典获取键的列表

karthikeya Boyini
更新于 2020年6月22日 14:08:38

6K+ 次浏览

设置字典元素 −Dictionary d = new Dictionary(); // 字典元素 d.Add(1, "One"); d.Add(2, "Two"); d.Add(3, "Three"); d.Add(4, "Four"); d.Add(5, "Five"); d.Add(6, "Six"); d.Add(7, "Seven"); d.Add(8, "Eight"); 要获取键,请使用列表集合 −List keys = new List(d.Keys); 循环遍历键并显示它们。以下是完整代码 −示例 在线演示using System; using System.Collections.Generic; public class Demo {    public static void Main() {       Dictionary d = new Dictionary();       // 字典元素       d.Add(1, "One");       d.Add(2, "Two");       d.Add(3, "Three");       d.Add(4, "Four"); ... 阅读更多

C# 程序:将多个字符串转换为单个逗号分隔的字符串

Samual Sam
更新于 2020年6月22日 14:09:08

370 次浏览

设置字符串 −List val = new List(); // 字符串列表 val.Add("water"); val.Add("food"); val.Add("air"); 使用 Join 方法将多个字符串转换为单个逗号分隔的字符串 −string.Join(",", val.ToArray()); 以下是完整代码 −示例 在线演示using System; using System.Collections.Generic; public class Demo {    public static void Main() {       List val = new List();       // 字符串列表       val.Add("water");       val.Add("food");       val.Add("air");       string res = string.Join(",", val.ToArray());       Console.WriteLine(res);    } }输出water,food,air

C# 程序:使用 Lambda 表达式查找数组中最小的元素

karthikeya Boyini
更新于 2020年6月22日 14:09:40

451 次浏览

声明一个数组 −int[] arr = { 10, 15, 5, 20}; 现在要从数组中获取最小元素,请使用带有 lambda 表达式的 Min() 方法 −arr.Min()); 以下是完整代码 −示例 在线演示using System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 10, 15, 5, 20};       Console.WriteLine(arr.Min(element => Math.Abs(element)));    } }输出5

C# 程序:查找列表中元素的索引

karthikeya Boyini
更新于 2020年6月22日 14:10:03

829 次浏览

设置一个列表并添加元素 −List val = new List(); // 整数元素 val.Add(35); val.Add(55); val.Add(68); 假设现在我们需要查找元素 68 的索引。为此,请使用 IndexOf() 方法 −int index = val.IndexOf(68); 以下是完整代码 −示例 在线演示using System; using System.Collections.Generic; public class Demo {    public static void Main() {       List val = new List();       // 整数元素       val.Add(35);       val.Add(55);       val.Add(68);       // 查找元素 68 的索引       int index = val.IndexOf(68);       Console.WriteLine(index);    } }输出2

C# 程序:使用数组中的元素创建列表

Samual Sam
更新于 2020年6月22日 13:58:10

364 次浏览

设置一个数组 −int[] val = new int[5]; // 整数元素 val[0] = 15; val[1] = 25; val[2] = 40; val[3] = 58; val[4] = 70; 现在设置一个列表并向其中添加数组 −List myList = new List(val); 以下是代码 −示例 在线演示using System; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] val = new int[5];       // 整数元素       val[0] = 15;       val[1] = 25;       val[2] = 40;       val[3] = 58;       val[4] = 70;       List myList = new List(val);       Console.WriteLine("元素...");       foreach(int res in myList) {          Console.WriteLine(res);       }       // 统计整数元素       Console.WriteLine("元素数量: "+myList.Count);    } }输出元素... 15 25 40 58 70 元素数量: 5

在 C# 中清除列表

karthikeya Boyini
更新于 2020年6月22日 13:58:40

381 次浏览

首先,设置一个列表 −List myList = new List(); myList.Add(45); myList.Add(77); 现在,要清除上述列表,请使用 Clear() −myList.Clear(); 以下是完整代码 −示例 在线演示using System; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add(45);       myList.Add(77);       Console.WriteLine("元素: "+myList.Count);       myList.Clear();       Console.WriteLine("使用 clear 后元素: "+myList.Count);    } }输出元素: 2 使用 clear 后元素: 0

C# 数组中的 TrueForAll() 方法

Samual Sam
更新于 2020年6月22日 13:59:12

113 次浏览

使用数组中的 TrueForAll() 方法,可以检查每个元素是否符合条件。让我们来看一个例子 −示例 在线演示using System; using System.Text; public class Demo {    public static void Main() {       int[] val = { 97, 45, 76, 21, 89, 45 };       // 检查所有数组元素是否都大于 1       bool result = Array.TrueForAll(val, res => res > 1);       Console.WriteLine(result);    } }输出True 使用数组中的 TrueForAll() 方法,可以检查每个元素是否符合条件。让我们来看一个例子 −示例 在线演示using System; ... 阅读更多

C# 程序:查找数组中最后一个匹配的元素

karthikeya Boyini
更新于 2020年6月22日 13:59:37

322 次浏览

要查找最后一个匹配的元素,请使用 Array.LastIndexOf 方法。如果元素不存在于整数数组中,则返回 -1。以下是数组 −int[] val = { 97, 45, 76, 21, 89, 45 }; 现在,假设您需要搜索元素 45 的最后一个索引。为此,请使用 Array.LastIndexOf() 方法 −int res = Array.LastIndexOf(val, 45); 以下是示例 −示例 在线演示using System; using System.Text; public class Demo {    public static void Main() {       int[] val = { 97, 45, 76, 21, 89, 45 };       // 元素 45 的最后一个索引       int res = Array.LastIndexOf(val, 45);       // 显示索引       Console.WriteLine("元素 45 的索引为 = "+res);    } }输出元素 45 的索引为 = 5

C# 程序:检查数组中是否存在值

Samual Sam
更新于 2020年6月22日 13:59:58

1K+ 次浏览

使用 Array.Exists 方法检查数组中是否存在值。设置一个字符串数组 −string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" }; 假设您需要在数组中查找值“keyboard”。为此,请使用 Array.Exists() −Array.Exists(strArray, ele => ele == "keyboard"); 如果元素存在,则返回 true 值,如下所示 −示例 在线演示using System; using System.Text; public class Demo {    public static void Main() {       string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" };       bool res1 = Array.Exists(strArray, ele => ele == "harddisk");   ... 阅读更多

广告
© . All rights reserved.