給定一個字符數組,任務是將該數組轉換為包含字符元素的 ASCII 值的IntStream。
例子:
Input: char[] = { 'G', 'e', 'e', 'k', 's' } Output: 71, 101, 101, 107, 115 Input: char[] = { 'G', 'e', 'e', 'k', 's', 'F', 'o', 'r', 'G', 'e', 'e', 'k', 's' } Output: 71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115
算法:
- 獲取要轉換的字符數組。
- 使用 Stream.of() 方法將其轉換為 Stream。
- 使用flatMapToInt()方法將形成的Stream轉換為IntStream。
- 打印形成的IntStream。
下麵是上述方法的實現:
// Java program to convert
// Character Array to IntStream
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// Get the Character Array to be converted
Character charArray[] = { 'G', 'e', 'e', 'k', 's' };
// Convert charArray to IntStream
IntStream
intStream
= Stream
// Convert charArray into Stream of characters
.of(charArray)
// Convert the Stream of characters into IntStream
.flatMapToInt(IntStream::of);
// Print the elements of the IntStream
System.out.println("IntStream:");
intStream.forEach(System.out::println);
}
}
輸出:
IntStream: 71 101 101 107 115
相關用法
- Java Character Array轉String用法及代碼示例
- Java Character codePointAt()用法及代碼示例
- Java Character codePointBefore()用法及代碼示例
- Java Character codePointCount()用法及代碼示例
- Java Character compare()用法及代碼示例
- Java Character digit()用法及代碼示例
- Java Character equals()用法及代碼示例
- Java Character forDigit()用法及代碼示例
- Java Character getDirectionality()用法及代碼示例
- Java Character getName()用法及代碼示例
- Java Character getNumericValue()用法及代碼示例
- Java Character getType(char ch)用法及代碼示例
- Java Character hashCode()用法及代碼示例
- Java Character highSurrogate()用法及代碼示例
- Java Character isAlphabetic()用法及代碼示例
- Java Character isBmpCodePoint()用法及代碼示例
- Java Character isDefined()用法及代碼示例
- Java Character isHighSurrogate()用法及代碼示例
- Java Character isISOControl()用法及代碼示例
- Java Character isIdentifierIgnorable()用法及代碼示例
- Java Character isJavaIdentifierPart()用法及代碼示例
- Java Character isJavaIdentifierstart()用法及代碼示例
- Java Character isJavaLetterOrDigit()用法及代碼示例
- Java Character isLetter()用法及代碼示例
- Java Character isLetterOrDigit()用法及代碼示例
注:本文由純淨天空篩選整理自RishabhPrabhu大神的英文原創作品 Java Program to convert Character Array to IntStream。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。