无法将char []转换为java 8中的流[重复]

问题描述 投票:6回答:5

这个问题在这里已有答案:

我正在编写一个程序,其中一个方法将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
java java-8 java-stream
5个回答
4
投票

Character wrapper class

您正在尝试制作一种原始类型的char流。使用wrapperCharacter尝试此代码。您将获得所需的输出。


8
投票

对于intlongdouble和引用类型(对象)类型的元素,只有四种流类型。因此,由于没有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));

7
投票

你不能用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));

3
投票

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()

2
投票

您可以使用静态方法IntStream.range(),它可以按如下方式用于创建索引流,然后使用它们将charArray中的值映射到Character-stream:

Stream<Character> chars = IntStream.range(0, charArray.length)
    .mapToObj(i -> charArray[i]);

使用.mapToObj()方法,因为我们在IntStream上运行,我们想要将未装箱的原始流转换为盒装参考流。

© www.soinside.com 2019 - 2024. All rights reserved.