找到 34423 篇文章 关于编程

如何在 Java 中获取字节数组的校验和?

karthikeya Boyini
更新于 2019-07-30 22:30:25

1K+ 次查看

创建您想要校验和的字节数组 -byte[] arr = "This is it!".getBytes();现在,创建一个校验和对象 -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();   ... 阅读更多

如何在 Java 中从字符串中提取多个整数?

Samual Sam
更新于 2019-07-30 22:30:25

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]

使用 Lambda 表达式获取单词频率的 Java 程序

karthikeya Boyini
更新于 2019-07-30 22:30:25

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()   ... 阅读更多

如何在 Java 中以科学计数法显示数字?

Samual Sam
更新于 2019-07-30 22:30:25

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");   ... 阅读更多

在 Java HashMap 中设置日期值?

karthikeya Boyini
更新于 2019-07-30 22:30:25

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));     ... 阅读更多

如何在 Java 中显示小于 20 的随机数

Samual Sam
更新于 2019-07-30 22:30:25

279 次查看

首先,创建一个 Random 类对象 -Random rand = new Random();现在,创建一个新数组 -int num; int arr[] = new int[10];循环遍历并设置 Random nextInt,参数为 20,因为您想要小于 20 的随机数 -for (int j = 0; j

如何在 Java 中计算范围内随机数重复的可能性

karthikeya Boyini
更新于 2019-07-30 22:30:25

154 次查看

要获取范围内随机数的重复数字,请循环遍历并创建两个 Random 类对象 -使用 nextInt() 获取下一个数字 -intrandVal1 = new Random().nextInt(50); intrandVal2 = new Random().nextInt(50);现在,比较以上两个数字 -if (randVal1 == randVal2) {    System.out.println("Duplicate number = "+randVal1); }以上所有操作都必须在循环中完成 -for (int i = 1; i

Java 程序检查 Float 是否是无限大或非数字 (NAN)

Samual Sam
更新于 2019-07-30 22:30:25

2K+ 次查看

要检查 Float 是否是无限大,请使用 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

Java 程序获取两个时间点之间的分钟数

karthikeya Boyini
更新于 2024-10-18 11:58:13

512 次查看

在本文中,我们将使用 Java 计算两个时间点之间的分钟数。这将通过使用 java.time 包中的 Instant 和 Duration 类来完成。我们将创建两个时间实例,向其中一个添加特定的小时和分钟,然后计算两者之间的分钟差。获取两个时间点之间分钟数的步骤以下是如何获取两个时间点之间分钟数的步骤 -首先,导入必要的类:java.time 包中的 Duration、Instant 和 ChronoUnit。使用 Instant.now() 创建当前时间的实例。向第一个时间实例添加 5 小时 10 分钟,然后... 阅读更多

Java 程序获取两个时间点之间的毫秒数

Samual Sam
更新于 2024-08-12 23:13:17

2K+ 次查看

在本文中,我们将演示如何使用 ChronoUnit 类计算两个 Instant 对象之间毫秒数的差异。它展示了如何在 java.time 包中使用基于时间类的,重点关注创建和操作时间实例以及计算它们之间的时间差。ChronoUnit:一组标准的日期和时间单位,允许您操作日期、时间和日期时间。这些单位(如年、月和日)旨在跨各种日历系统工作,尽管具体规则可能略有不同。您还可以通过实现 TemporalUnit 接口来扩展这些单位。问题陈述计算... 阅读更多

广告

© . All rights reserved.