找到 34423 篇文章,主题为编程
3K+ 次查看
假设我们有一个包含 n 个元素的数组 A。元素已排序。我们必须找到最接近给定整数的值。数组可能包含重复值和负数。因此,如果数组类似于 [2, 5, 6, 7, 8, 8, 9],目标数字是 4,则最接近的元素是 5。我们可以通过遍历给定数组并跟踪当前元素与每个元素的绝对差来解决这个问题。最后返回具有最小绝对差的元素。示例 在线演示 #include #include using namespace std; int getNearest(int x, int y, int target) { ... 阅读更多
895 次查看
类型推断是指任何表达式的类型。例如,编译器可以自动理解方法的返回类型或参数类型。由于 Java 已经知道函数接口的单个抽象方法的预期参数类型,因此可以省略参数列表中的类型。语法 (var1, var2 …) -> { 方法体 }在下面的示例中,我们可以按字符串的最后一个字符对 String[] 数组进行排序。示例 import java.util.Arrays; public class TypeInferencingLambdaTest { public static void main(String[] args) { String[] names = {"Raja", "Jai", "Adithya", "Surya", "Chaitanya", "Ravi", "Krishna"}; Arrays.sort(names, (s1, ... 阅读更多
2K+ 次查看
仅定义了一个抽象方法的接口称为函数式接口。不必使用 @FunctionalInterface 注解标记函数式接口,编译器不会抛出任何错误。但是,最好使用 @FunctionalInterface 注解,以避免意外添加额外的方法。如果用 @FunctionalInterface 注解的接口包含多个抽象方法,则会抛出编译时错误。语法 @FunctionalInterface interface interface_name { // 只有一个抽象方法声明 }示例 @FunctionalInterface interface Shape { void printArea(int x); } public class SquareTest { public static void main (String args[]) { Shape square ... 阅读更多
80 次查看
要获取数组指定维度中元素的总数,代码如下所示:示例 在线演示 using System; public class Demo { public static void Main() { string[] products = new string[] { "Andy", "Mark", "Gary", "Andre"}; Console.WriteLine("一个或多个名称以 'A' 开头?= {0}", Array.Exists(products, ele => ele.StartsWith("A"))); Console.WriteLine("数组是否具有固定大小?= " + products.IsFixedSize); Console.WriteLine("数组是否只读?= " + products.IsReadOnly); Console.WriteLine("数组是否已同步?= " + products.IsSynchronized); ... 阅读更多
95 次查看
要将 SortedList 对象的容量设置为实际元素数量,代码如下所示:示例 在线演示 using System; using System.Collections; public class Demo { public static void Main(String[] args) { SortedList sortedList = new SortedList(); sortedList.Add("A", "1"); sortedList.Add("B", "2"); sortedList.Add("C", "3"); sortedList.Add("D", "4"); sortedList.Add("E", "5"); sortedList.Add("F", "6"); sortedList.Add("G", "7"); sortedList.Add("H", "8"); sortedList.Add("I", "9"); sortedList.Add("J", "10"); Console.WriteLine("SortedList 元素..."); ... 阅读更多
94 次查看
要将 ArrayList 的容量设置为实际元素数量,代码如下所示:示例 在线演示 using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list1 = new ArrayList(); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); list1.Add("E"); list1.Add("F"); list1.Add("G"); list1.Add("H"); list1.Add("I"); Console.WriteLine("ArrayList1 中的元素..."); foreach (string res in list1) { Console.WriteLine(res); ... 阅读更多
160 次查看
要搜索满足条件的元素并返回整个列表中最后一次出现的基于零的索引,代码如下所示:示例 在线演示 using System; using System.Collections.Generic; public class Demo { private static bool demo(int i) { return ((i % 10) == 0); } public static void Main(String[] args) { List
115 次查看
要搜索集合中指定对象的索引,代码如下所示:示例 在线演示 using System; using System.Collections.Specialized; public class Demo { public static void Main() { StringCollection strCol = new StringCollection(); strCol.Add("Accessories"); strCol.Add("Books"); strCol.Add("Electronics"); strCol.Add("Books"); Console.WriteLine("StringCollection 元素..."); foreach (string res in strCol) { Console.WriteLine(res); } strCol.Insert(2, "Headphone"); Console.WriteLine("StringCollection 元素...已更新"); foreach (string res in strCol) { Console.WriteLine(res); ... 阅读更多
65 次查看
获取BitArray中元素数量的代码如下所示:示例 在线演示using System; using System.Collections; public class Demo { public static void Main() { BitArray arr1 = new BitArray(5); BitArray arr2 = new BitArray(5); arr1[0] = false; arr1[1] = false; arr2[0] = false; arr2[1] = true; Console.WriteLine("BitArray1 元素..."); foreach (bool res in arr1) { Console.WriteLine(res); } Console.WriteLine("BitArray2 元素..."); ... 阅读更多
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP