Java 如何使用Lambda将字符串数组转换为整数数组?
我试图使用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
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];
}