Increment Character in Java – Java 中的递增字符

最后修改: 2023年 10月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll learn how to generate a sequence of characters from ‘A’ to ‘Z’ in Java. We’ll do this by incrementing ASCII values.

在本教程中,我们将学习如何在 Java 中生成从 “A “到 “Z “的字符序列。我们将通过递增 ASCII 值来实现。

We’ll be generating a sequence of characters using a for loop and IntStream from the Java 8 Stream API.

我们将使用 for 循环和来自 Java 8 Stream APIIntStream 生成字符序列。

2. Using a for Loop

2.使用 for 循环

We’ll create a list of capital letters from ‘A‘ to ‘Z‘ using a standard for loop:

我们将使用标准的 for 循环创建一个从”A“到”Z“的大写字母列表:

@Test 
void whenUsingForLoop_thenGenerateCharacters(){
    List<Character> allCapitalCharacters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  
    List<Character> characters = new ArrayList<>();
    for (char character = 'A'; character <= 'Z'; character++) {
        characters.add(character);   
    }
  
    Assertions.assertEquals(alphabets, allCapitalCharacters);
}

Every letter has a unique number in the ASCII system. For example, ‘A’ is represented as 65, ‘B’ as 66, and ‘Z’ as 90.

在 ASCII 系统中,每个字母都有一个唯一的数字。例如,”A “代表 65,”B “代表 66,”Z “代表 90。

In the example above, we first increment the number of each letter in a for loop. Then, we convert it to the corresponding ASCII letter.

在上面的示例中,我们首先在 for 循环中递增每个字母的数字。然后,我们将其转换为相应的 ASCII 字母。

Finally, by using the assertEquals() method of the Assertions class, we check if the generated list matches the expected list of all capital characters.

最后,通过使用 Assertions 类的 assertEquals() 方法,我们可以检查生成的列表是否与所有大写字符的预期列表相匹配。

3. Using Java 8 IntStream

3.使用 Java 8 IntStream

Using Java 8 IntStream, we can generate a sequence of all capital letters from ‘A’ to ‘Z’:

使用 Java 8 IntStream,我们可以生成从 “A “到 “Z “的所有大写字母序列:

@Test
void whenUsingStreams_thenGenerateCharacters() {
    List<Character> allCapitalCharacters = Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

    List<Character> characters = IntStream.rangeClosed('A', 'Z')
      .mapToObj(c -> (char) c)
      .collect(Collectors.toList());
    Assertions.assertEquals(characters, allCapitalCharacters);
}

In the above example, employing IntStream from Java 8, we generate characters ranging from ‘A’ to ‘Z’ with ASCII values spanning 65 to 90.

在上面的示例中,我们使用 Java 8 中的 IntStream 生成了从 “A “到 “Z “的字符,ASCII 值从 65 到 90 不等。

Initially, we map these values to characters and then subsequently collect them into a list.

首先,我们将这些值映射为字符,然后将它们收集到一个列表中。

To conclude, we employ the assertEquals() method from the Assertions class to verify if the generated list aligns with the expected list of all capital letters.

最后,我们使用 Assertions 类中的 assertEquals() 方法来验证生成的列表是否与预期的全部大写字母列表一致。

4. Conclusion

4.结论

In this short article, we explored how we can use the Stream API and for loop to increment the ASCII values of the characters and print their corresponding values.

在这篇短文中,我们探讨了如何使用流 API 和 for 循环来递增字符的 ASCII 值并打印其相应值。

As usual, the source code for the examples is available over on GitHub.

与往常一样,这些示例的源代码可在 GitHub 上获取。