找到 34423 篇文章 关于编程
108 次浏览
要获得对数组的同步访问,代码如下所示:示例 在线演示 using 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 ... 阅读更多
198 次浏览
要将对象添加到集合的末尾,代码如下所示:示例 using System; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection<int> col = new Collection<int>(); 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("集合是否包含... 阅读更多
71 次浏览
要将字符串添加到 StringCollection 的末尾,代码如下所示:示例 using 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); ... 阅读更多
524 次浏览
要获取列表中所有满足谓词指定条件的元素,代码如下所示:示例 using 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<int> list = new List<int>(); 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) { ... 阅读更多
210 次浏览
要获取哈希表中指定键的哈希码,代码如下所示:示例 在线演示 using 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} ", ... 阅读更多
119 次浏览
要获得对 ListDictionary 的同步访问,代码如下所示:示例 在线演示 using 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 元素..."); ... 阅读更多
7K+ 次浏览
java.util.function 包定义了一些内置函数式接口,这些接口可以在创建 lambda 表达式或方法引用时使用。内置函数式接口:1) 函数接口函数接口只有一个方法 apply()。它可以接受任何数据类型的对象并返回任何数据类型的结果。示例 import java.util.*; import java.util.function.*; public class FunctionTest { public static void main(String args[]) { String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies", "Scotland"}; Function<String[], String> converter = (all) -> { // lambda 表达式 String names = ""; ... 阅读更多
240 次浏览
构造函数引用类似于方法引用,只是方法名称为“new”。它可以使用“类名”和关键字“new”以及以下语法创建。语法 :: new在下面的示例中,我们使用 java.util.function.Function。它是一个函数式接口,其唯一的抽象方法是 apply()。Function 接口表示一个操作,该操作接受单个参数 T 并返回结果 R。示例 import java.util.function.*; @FunctionalInterface interface MyFunctionalInterface { Employee getEmployee(String name); } class Employee { private String name; public Employee(String name) { this.name = name; } public String ... 阅读更多
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 = ... 阅读更多
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 ... 阅读更多
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP