Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Lambda将字符串数组转换为整数数组?_Java_Arrays_Lambda_Java 8 - Fatal编程技术网

Java 如何使用Lambda将字符串数组转换为整数数组?

Java 如何使用Lambda将字符串数组转换为整数数组?,java,arrays,lambda,java-8,Java,Arrays,Lambda,Java 8,我试图使用lambda表达式将字符串数组转换为整数数组 我在下面提供了我的代码,并简要描述了我迄今为止所做的尝试: String [] inputData = userInput.split("\\s+"); Integer [] toSort = new Integer [] {}; try { toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseInt).toArray(); }catch(NumberF

我试图使用lambda表达式将字符串数组转换为整数数组

我在下面提供了我的代码,并简要描述了我迄今为止所做的尝试:

String [] inputData = userInput.split("\\s+");
Integer [] toSort = new Integer [] {};
try {
    toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseInt).toArray();
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
}
我上面的lamba表达式映射到int数组,这不是我想要的,编译此代码时,我得到以下错误消息:

BubbleSort.java:13: error: incompatible types: int[] cannot be converted to Integer[]
            toSort = Arrays.asList(inputData).stream().mapToInt(Integer::parseIn
t).toArray();

          ^
1 error

有没有一种简单的方法可以让我使用lambdas或其他方法从字符串数组转换为整数数组?

如果你想要一个
整数
数组,不要映射到
IntStream
,映射到

mapToInt(Integer::parseInt).toArray()
返回
int[]
数组,因为
matpoint
生成
IntStream
但是
int[]
数组不能被
Integer[]
引用使用(装箱仅对基本类型有效,而数组不是)

你能用的是

import java.util.stream.Stream;
//...
toSort = Stream.of(inputData)
               .map(Integer::valueOf) //value of returns Integer, parseInt returns int
               .toArray(Integer[]::new); // to specify type of returned array

正如其他人已经指出的,
mapToInt
返回一个
IntStream
,其
toArray
方法将返回
int[]
而不是
Integer[]
。除此之外,还有一些其他方面需要改进:

Integer [] toSort = new Integer [] {};
是初始化数组的一种不必要的复杂方法。使用其中一个

Integer[] toSort = {};

但是,如果无论如何都要覆盖它,就不应该初始化它。如果要为异常情况设置回退值,请在异常处理程序内进行赋值:

String[] inputData = userInput.split("\\s+");
Integer[] toSort;
try {
    toSort = Arrays.stream(inputData).map(Integer::parseInt).toArray(Integer[]::new);
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
    toSort=new Integer[0];
}
此外,请注意,在您的案例中不需要
字符串[]
数组:

Integer[] toSort;
try {
    toSort = Pattern.compile("\\s+").splitAsStream(userInput)
        .map(Integer::parseInt).toArray(Integer[]::new);
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
    toSort=new Integer[0];
}

Pattern
指的是
String.split
内部使用的同一类。

这将返回另一个BubbleSort.java:13:error:不兼容类型:Object[]无法转换为I nteger[]错误,对于我来说,类似于第一个:
BubbleSort.java:13:error:不兼容类型:Object[]无法转换为I nteger[]
@JamesZafar抱歉。我使用了错误的
toArray
方法。现已修复。这在变量
Stream
BubbleSort.java:13:错误:找不到符号。。。symbol:variable Stream
是否需要导入某些内容才能使此方法正常工作?@JamesZafar是的,您需要将导入添加到
java.util.Stream.Stream。您可能应该开始使用IDE,它将允许您自动添加导入。或者
IntStream.boxed()
@bayou.io:当然可以,但是使用
mapToInt
后接
boxed()会有点荒谬
当您可以首先使用
map
时。@Pshemo:无论您是使用
.map(Integer::valueOf)
还是
.map(Integer::parseInt)
;两者都将首先解析为
int
,然后将其包装为
整数。唯一重要的是你是使用
map
还是
mapToInt
。只需使用for循环…但是如果你不介意对
int[]
进行排序,而不是
Integer[]
,你就可以接受
.mapToInt(Integer::parseInt).toArray()
的结果,并将
toSort
的类型更改为
int[]
String[] inputData = userInput.split("\\s+");
Integer[] toSort;
try {
    toSort = Arrays.stream(inputData).map(Integer::parseInt).toArray(Integer[]::new);
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
    toSort=new Integer[0];
}
Integer[] toSort;
try {
    toSort = Pattern.compile("\\s+").splitAsStream(userInput)
        .map(Integer::parseInt).toArray(Integer[]::new);
}catch(NumberFormatException e) {
    System.out.println("Error. Invalid input!\n" + e.getMessage());
    toSort=new Integer[0];
}