我正在编写一段程序,其中一个方法以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()的第二行编译通过,而在main方法的第三行没有编译通过。请解释这种行为背后的原因是什么?
以下是提到的编译错误 -
/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