这个问题在这里已有答案:
我正在编写一个程序,其中一个方法将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;
}
主要方法如下 -
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);
}
方法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
对于int
,long
,double
和引用类型(对象)类型的元素,只有四种流类型。因此,由于没有char
流,Arrays.stream
不能应用于char[]
阵列。
一个字符流的规范表示是一个IntStream
并获得它没有复制或拳击开销工作,如
CharBuffer.wrap(charArray).chars().forEach(c -> System.out.println((char)c));
由于int
值的默认解释是“整数”,因此您需要使用lambda表达式将其转换为char
以将其解释为“character”。
如果您不需要在字符之间进行换行,则只需通过一行打印整个数组即可
System.out.println(String.valueOf(charArray));
你不能用CharStream
创建一个Arrays.stream(outputTwoChits)
,因为在Arrays类中没有超载需要char[]
并返回CharStream
(事实上根本没有CharStream
)并且做Stream.of(outputTwoChits)
不会提供你在这种特殊情况下所期望的。
但是,由于您只是出于打印目的,您可以执行以下操作:
Arrays.stream(new String(outputTwoChits).split("")).forEach(System.out::println);
或者从String
数组中构造一个char
对象,并在其上调用chars
方法,该方法应生成IntStream
,然后在打印到控制台之前从int
投射到char
。
new String(outputTwoChits).chars().forEach(c -> System.out.println((char)c));
在
Arrays.stream(inputTwoChits)
你将char[][]
数组传递给<T> Stream<T> stream(T[] array)
,这很好,因为char[]
是一个参考类型。
另一方面,在
Arrays.stream(outputTwoChits)
你传递的是char[]
,char
不是引用类型,所以它与<T> Stream<T> stream(T[] array)
的签名不符。
您可以使用以下方法从IntStream
生产char[]
:
new String(outputTwoChits).chars()
您可以使用静态方法IntStream.range()
,它可以按如下方式用于创建索引流,然后使用它们将charArray
中的值映射到Character
-stream:
Stream<Character> chars = IntStream.range(0, charArray.length)
.mapToObj(i -> charArray[i]);
使用.mapToObj()
方法,因为我们在IntStream
上运行,我们想要将未装箱的原始流转换为盒装参考流。