找到 34423 篇文章,关于编程
1K+ 次浏览
创建一个你想要计算校验和的字节数组 −byte[] arr = "This is it!".getBytes();现在,创建一个 Checksum 对象 −Checksum checksum = new Adler32(); checksum.update(arr, 0, arr.length);上面的 update() 使用指定的字节数组更新当前校验和。现在,使用 getValue() 方法获取校验和,该方法返回当前校验和值。示例 在线演示import java.util.zip.Adler32; import java.util.zip.Checksum; public class Demo { public static void main(String[] argv) throws Exception { byte[] arr = "This is it!".getBytes(); Checksum checksum = new Adler32(); checksum.update(arr, 0, arr.length); long res = checksum.getValue(); ... 阅读更多
742 次浏览
假设我们有以下包含整数和字符的字符串 −String str = "(29, 12; 29, ) (45, 67; 78, 80)";现在,要提取整数,我们将使用以下模式 −\d我们已将其设置为 Pattern 类 −Matcher matcher = Pattern.compile("\d+").matcher(str);示例 在线演示import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String[] args) { String str = "(29, 12; 29, ) (45, 67; 78, 80)"; Matcher matcher = Pattern.compile("\d+").matcher(str); Listlist = new ArrayList(); while(matcher.find()) { list.add(Integer.parseInt(matcher.group())); } System.out.println("Integers = "+list); } }输出Integers = [29, 12, 29, 45, 67, 78, 80]
435 次浏览
假设以下是列表 −List list = Arrays.asList("Welcome", "to", "the", "club", "club", "the");现在,让我们创建一个 Map 来获取单词的频率。在这里,我们也使用 Lambda 表达式 −Map map = list .parallelStream() .flatMap(a -> Arrays.asList(a.split(" ")).stream()) .collect( Collectors.toConcurrentMap(c ->c.toLowerCase(), c -> 1, Integer::sum));示例 在线演示import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Demo { public static void main(String[] args) { List list = Arrays.asList("Welcome", "to", "the", "club", "club", "the"); Map map = list .parallelStream() ... 阅读更多
5K+ 次浏览
要以科学计数法显示数字,首先创建一个 NumberFormat 对象 −NumberFormat numFormat = newDecimalFormat();现在,假设你需要格式化 Integer 的最小值 −int i = Integer.MIN_VALUE; System.out.println(i); numFormat = newDecimalFormat("0.######E0"); System.out.println(numFormat.format(i)); numFormat = newDecimalFormat("0.#####E0"); System.out.println(numFormat.format(i));上面,我们使用了 NumberFormat 类的 format() 方法。示例 在线演示import java.text.DecimalFormat; import java.text.NumberFormat; public class Demo { public static void main(String args[]) { NumberFormat numFormat = new DecimalFormat(); int i = Integer.MIN_VALUE; System.out.println(i); numFormat = new DecimalFormat("0.######E0"); System.out.println(numFormat.format(i)); numFormat = new DecimalFormat("0.#####E0"); ... 阅读更多
1K+ 次浏览
创建一个 Calendar 实例和 Date 对象 −Calendar cal = Calendar.getInstance(); Date date = new Date(); cal.setTime(date);现在,创建一个 HashMap 并存储 Date 值 −LinkedHashMaphashMap = new LinkedHashMap(); hashMap.put("year", cal.get(Calendar.YEAR)); hashMap.put("month", cal.get(Calendar.MONTH)); hashMap.put("day", cal.get(Calendar.DAY_OF_MONTH));示例 在线演示import java.util.Calendar; import java.util.Date; import java.util.LinkedHashMap; public class Demo { public static void main(String[] argv) { Calendar cal = Calendar.getInstance(); Date date = new Date(); System.out.println("Date = "+date); cal.setTime(date); LinkedHashMaphashMap = new LinkedHashMap(); hashMap.put("year", cal.get(Calendar.YEAR)); hashMap.put("month", cal.get(Calendar.MONTH)); hashMap.put("day", cal.get(Calendar.DAY_OF_MONTH)); ... 阅读更多
2K+ 次浏览
要检查浮点数是否为 isInfinite,请使用 isInfinite() 方法;要检查 NAN,请使用 isNaN() 方法。示例 在线演示public class Demo { public static void main(String[] args) { float value1 = (float) 1 / 0; boolean res1 = Float.isInfinite(value1); System.out.println("Checking for isInfinite? = "+res1); float value2 = (float) Math.sqrt(9); boolean res2 = Float.isNaN(value2); System.out.println("Checking for isNan? = "+res2); } }输出Checking for isInfinite? = true Checking for isNan? = false
512 次浏览
在本文中,我们将使用 Java 计算两个时间点之间的分钟数。这将通过使用 java.time 包中的 Instant 和 Duration 类来完成。我们将创建两个时间实例,向其中一个添加特定的小时和分钟,然后计算两个时间之间的分钟差。获取两个时间点之间分钟数的步骤获取两个时间点之间分钟数的步骤如下:首先,从 java.time 包中导入必要的类:Duration、Instant 和 ChronoUnit。使用 Instant.now() 创建当前时间的实例。向第一个时间实例添加 5 小时 10 分钟,然后... 阅读更多
2K+ 次浏览
本文将演示如何使用 ChronoUnit 类计算两个 Instant 对象之间以毫秒为单位的差值。它展示了如何使用 java.time 包中的基于时间的类,特别是关注创建和操作时间点以及计算它们之间持续时间的方法。ChronoUnit:一组标准的日期和时间单位,允许您操作日期、时间和日期时间。这些单位,例如年、月和日,旨在跨各种日历系统工作,尽管具体规则可能略有不同。您还可以通过实现 TemporalUnit 接口来扩展这些单位。问题陈述 计算…… 阅读更多
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP