找到 34423 篇文章 相关编程

如何在 C# 中获取数组的同步访问?

AmitDiwan
更新于 2019-12-16 06:35:10

108 次浏览

要获取数组的同步访问,代码如下:示例 在线演示使用 System; public class Demo {    public static void Main() {       int[] intArr = new int[] {5, 10, 15, 20, 25, 30, 35, 40 };       Console.WriteLine("整数数组...");       foreach (int i in intArr)       Console.WriteLine(i);       Console.WriteLine("在数组上应用锁后...");       lock(intArr.SyncRoot) {          foreach (Object ob in intArr)          Console.WriteLine(ob);       }    } }输出这将产生以下输出:整数 数组... 5 ... 阅读更多

在 C# 中将对象添加到集合的末尾

AmitDiwan
更新于 2019-12-16 06:29:59

198 次浏览

要将对象添加到集合的末尾,代码如下:示例使用 System; using System.Collections.ObjectModel; public class Demo {    public static void Main() {       Collection col = new Collection();       col.Add(10);       col.Add(20);       col.Add(30);       col.Add(40);       col.Add(50);       col.Add(60);       col.Add(70);       col.Add(80);       Console.WriteLine("集合中的元素...");       foreach(int val in col) {          Console.WriteLine(val);       }       Console.WriteLine("集合是否包含... 阅读更多

在 C# 中将字符串添加到 StringCollection 的末尾

AmitDiwan
更新于 2019-12-16 06:26:11

71 次浏览

要将字符串添加到 StringCollection 的末尾,代码如下:示例使用 System; using System.Collections.Specialized; public class Demo {    public static void Main() {       StringCollection strCol = new StringCollection();       strCol.Add("John");       strCol.Add("Tim");       strCol.Add("Gary");       strCol.Add("Katie");       strCol.Add("Andy");       strCol.Add("Amy");       strCol.Add("Scarlett");       strCol.Add("Jacob");       Console.WriteLine("StringCollection 中的元素...");       foreach(Object ob in strCol) {          Console.WriteLine(ob);       }       Console.WriteLine("元素数量 = "+strCol.Count); ... 阅读更多

如何在 C# 中获取列表中所有满足谓词指定条件的元素?

AmitDiwan
更新于 2019-12-16 06:22:50

524 次浏览

要获取列表中所有满足谓词指定条件的元素,代码如下:示例使用 System; using System.Collections.Generic; public class Demo {    private static bool demo(int i) {       return ((i % 3) == 0);    }    public static void Main(String[] args) {       List list = new List();       list.Add(9);       list.Add(15);       list.Add(20);       list.Add(40);       list.Add(50);       list.Add(60);       Console.WriteLine("列表元素...");       foreach (int i in list) {   ... 阅读更多

如何在 C# 中获取哈希表中指定键的哈希码?

AmitDiwan
更新于 2019-12-16 06:19:21

210 次浏览

要获取哈希表中指定键的哈希码,代码如下:示例 在线演示使用 System; using System.Collections; public class HashCode : Hashtable {    public static void Main(string[] args) {       HashCode hash = new HashCode();       hash.Add("A", "Jacob");       hash.Add("B", "Mark");       hash.Add("C", "Tom");       hash.Add("D", "Nathan");       hash.Add("E", "Tim");       hash.Add("F", "John");       hash.Add("G", "Gary");       Console.WriteLine("键值对...");       foreach(DictionaryEntry entry in hash) {          Console.WriteLine("{0} and {1} ", ... 阅读更多

如何在 C# 中获取 ListDictionary 的同步访问?

AmitDiwan
更新于 2019-12-16 06:15:05

119 次浏览

要获取 ListDictionary 的同步访问,代码如下:示例 在线演示使用 System; using System.Collections; using System.Collections.Specialized; public class Demo {    public static void Main() {       ListDictionary dict = new ListDictionary();       dict.Add("1", "SUV");       dict.Add("2", "Sedan");       dict.Add("3", "Utility Vehicle");       dict.Add("4", "Compact Car");       dict.Add("5", "SUV");       dict.Add("6", "Sedan");       dict.Add("7", "Utility Vehicle");       dict.Add("8", "Compact Car");       dict.Add("9", "Crossover");       dict.Add("10", "Electric Car");       Console.WriteLine("ListDictionary 元素...");       ... 阅读更多

Java 中的内置函数式接口有哪些?

raja
更新于 2020-07-10 14:00:23

7K+ 次浏览

java.util.function 包定义了一些内置函数式接口,这些接口可以在创建 lambda 表达式或方法引用时使用。内置函数式接口:1) 函数接口Function 接口只有一个 apply() 方法。它可以接受任何数据类型的对象并返回任何数据类型的结果。示例导入 java.util.*; 导入 java.util.function.*; 公共类 FunctionTest {    public static void main(String args[]) {       String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies", "Scotland"};       Function converter = (all) -> {  // lambda 表达式          String names = "";       ... 阅读更多

Java 中的构造器引用是什么?

raja
更新于 2020-07-10 13:55:56

240 次浏览

构造器引用类似于方法引用,只是方法名称为“new”。它可以通过使用“类名”和关键字“new”以及以下语法创建。语法:: new在下面的示例中,我们使用 java.util.function.Function。它是一个函数式接口,其单个抽象方法是 apply()。Function 接口表示一个操作,该操作接受单个参数 T 并返回结果 R。示例导入 java.util.function.*; @FunctionalInterface 接口 MyFunctionalInterface {    Employee getEmployee(String name); } 类 Employee {    private String name;    public Employee(String name) {       this.name = name;    }    public String ... 阅读更多

如何在 Java 中使用 lambda 表达式实现监听器?

raja
更新于 2020-07-10 13:50:30

1K+ 次浏览

当我们在 Java 中使用 lambda 表达式作为监听器时,我们不必显式地实现 ActionListener 接口。相反,我们可以使用以下语法。语法button.addActionListener(e -> { //一些语句 });ActionListener 接口只定义了一个方法 actionPerformed()。它是一个函数式接口,这意味着可以使用 lambda 表达式来替换代码。示例import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LambdaListenerTest extends JFrame {    public static void main(String args[]) {       new LambdaListenerTest();    }    private JButton button;    public ClickMeLambdaTest() {       setTitle("Lambda 表达式测试");       button = ... 阅读更多

如何在 Java 中的方法中传递 lambda 表达式?

raja
更新于 2020年7月10日 12:50:58

3K+ 浏览量

lambda 表达式可以传递给一个方法,该方法的参数类型为函数式接口。如果需要将 lambda 表达式作为参数传递,则接收 lambda 表达式参数的参数类型必须为函数式接口类型。在下面的示例中,lambda 表达式可以传递给一个方法,该方法的参数类型为“TestInterface”。示例interface TestInterface {    boolean test(int a); } class Test {    // lambda 表达式可以作为 check() 方法的第一个参数传递    static boolean check(TestInterface ti, int b) {       return ti.test(b);    } } public class ... 阅读更多

广告
© . All rights reserved.