ash*_*164 6 java java-8 java-stream
我正在编写一个程序,其中一个方法将char [] []作为输入并返回char [].方法如下 -
private static char[] getTableFromTwoChits(char[][] inputTwoChits) {
Map<Character, Character> map = new HashMap<>();
Arrays.stream(inputTwoChits).forEach(x -> map.put(x[0], x[1]));
map.entrySet().forEach(System.out::println);
char[] result = new char[inputTwoChits.length+1]; int index=0;
char startPoint = inputTwoChits[0][0];
do {
result[index] = startPoint;index++;
startPoint = map.get(startPoint);
}while(startPoint != inputTwoChits[0][0]);
result[index] = startPoint;
return result;
}
Run Code Online (Sandbox Code Playgroud)
主要方法如下 -
public static void main(String[] args) {
char[][] inputTwoChits = {{'A','B'},{'C','D'},{'B','C'},{'E','F'},{'F','A'},{'D','E'}};
char[] outputTwoChits = getTableFromTwoChits(inputTwoChits);
Arrays.stream(outputTwoChits).forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
方法getTableFromTwoChits()中的第2行正在编译,而main方法的第3行没有编译.
请解释这种行为背后的原因是什么?
编译错误如下所述 -
/CircularTableTwoChits.java:21: error: no suitable method found for stream(char[])
Arrays.stream(outputTwoChits).forEach(System.out::println);
^
method Arrays.<T#1>stream(T#1[]) is not applicable
(inference variable T#1 has incompatible bounds
equality constraints: char
upper bounds: Object)
method Arrays.<T#2>stream(T#2[],int,int) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
method Arrays.stream(int[]) is not applicable
(argument mismatch; char[] cannot be converted to int[])
method Arrays.stream(long[]) is not applicable
(argument mismatch; char[] cannot be converted to long[])
method Arrays.stream(double[]) is not applicable
(argument mismatch; char[] cannot be converted to double[])
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>stream(T#1[])
T#2 extends Object declared in method <T#2>stream(T#2[],int,int)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
Run Code Online (Sandbox Code Playgroud)
只有四个流类型,类型的元素int,long,double,和引用类型(对象).所以,由于没有char流,Arrays.stream不能应用于char[]数组.
字符流的规范表示是一个IntStream并且在没有复制或装箱工作的情况下获取它
CharBuffer.wrap(charArray).chars().forEach(c -> System.out.println((char)c));
Run Code Online (Sandbox Code Playgroud)
由于int值的默认解释是"整数",因此需要使用lambda表达式将其转换char为"字符".
如果您不需要在字符之间进行换行,则只需通过一行打印整个数组即可
System.out.println(String.valueOf(charArray));
Run Code Online (Sandbox Code Playgroud)
你不能创建一个CharStreamwith, Arrays.stream(outputTwoChits) 因为Arrays类中没有重载,它接受a char[]并返回a CharStream(事实上根本就没有CharStream),并且Stream.of(outputTwoChits)在这种特殊情况下不会提供你所期望的.
但是,由于您只是出于打印目的,您可以执行以下操作:
Arrays.stream(new String(outputTwoChits).split("")).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
或构造一个String从对象char阵列,并调用chars应该产生在其上的方法IntStream从再项目int到char之前打印到控制台.
new String(outputTwoChits).chars().forEach(c -> System.out.println((char)c));
Run Code Online (Sandbox Code Playgroud)
在
Arrays.stream(inputTwoChits)
Run Code Online (Sandbox Code Playgroud)
您将char[][]数组传递给<T> Stream<T> stream(T[] array),这很好,因为char[]是引用类型。
另一方面,在
Arrays.stream(outputTwoChits)
Run Code Online (Sandbox Code Playgroud)
您传递的是char[], ,并且char不是引用类型,因此它与 的签名不匹配<T> Stream<T> stream(T[] array)。
您可以使用IntStream以下命令生成一个char[]:
new String(outputTwoChits).chars()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1557 次 |
| 最近记录: |