java.lang.ArrayStoreException异常的解决⽅案
java.lang.ArrayStoreException异常
异常提⽰
java.lang.ArrayStoreException: java.lang.Boolean
at java.util.stream.Nodes$FixedNodeBuilder.accept(Nodes.java:1222)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.pyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:545)
at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
at java.util.Array(ReferencePipeline.java:438)
at
查询百度的解释:试图将错误类型的对象存储到⼀个对象数组时抛出的异常。之后,在看看⾃⼰错误的代码:
Field[] filterCopyFields = Stream.of(appendFields)
.map(f -> !Name())).toArray(Field[]::new);
很容易看出问题的所在,这⾥我是想过滤Field[]数组中的元素,!Name())这个是过滤条件,发现了这⾥使⽤的居然是map,过滤应该是使⽤filter,map中的元素应该是返回结果并在toArray⽅法中转换成数组,这⾥map中返回的是Boolean布尔类型的数据,也就是说不能将boolean类型的对象存储到Field对象数组中。
这⾥可以看⼀下JDK8源码中对toArray(IntFunction<A[]> generator)⽅法的定义:
/**
* Returns an array containing the elements of this stream, using the
* provided {@code generator} function to allocate the returned array, as
* well as any additional arrays that might be required for a partitioned
* execution or for resizing.
*
* <p>This is a <a href="package-summary.html#StreamOps" rel="external nofollow" >terminal
* operation</a>.
*
* @apiNote
* The generator function takes an integer, which is the size of the
* desired array, and produces an array of the desired size.  This can be
* concisely expressed with an array constructor reference:
* <pre>{@code
*    Person[] men = people.stream()
*                          .filter(p -> p.getGender() == MALE)
*                          .toArray(Person[]::new);
* }</pre>
*
* @param <A> the element type of the resulting array
* @param generator a function which produces a new array of the desired
*                  type and the provided length
* @return an array containing the elements in this stream
* @throws ArrayStoreException if the runtime type of the array returned
* from the array generator is not a supertype of the runtime type of every
* element in this stream
*/
<A> A[] toArray(IntFunction<A[]> generator);
可以看到toArray()的参数是IntFunction<A[]>类型,从@param A the element type of the resulting array这个注解中可以看
到,A是表⽰返回数组的元素类型,在我的例⼦中返回类型是⼀个Field,⽽如果Stream中使⽤了map遍历,返回的类型⼜是Boolean,类型不匹配⽽出现错误。
解决更改
Field[] filterCopyFields = Stream.of(appendFields)
.filter(f -> !Name())).toArray(Field[]::new);
其实这种⼩问题应该很容易避免,在出现ArrayStoreException异常时应该对应着数组中的元素类型去
查错误,构造数组时应按照正确的类型来构造。
Java⼯具类List的toArray⽅法及java.lang.ArrayStoreException
1.List接⼝中有两个⽅法
Object[] toArray();
T[] toArray(T[] a);
分析:不带参数的⽅法默认是把数组转换为Object类型,⽽带参数的⽅法会将数组转换为指定的类型;
指定⽬标数组数据类型:
List<Integer> list = new ArrayList<Integer>();
list.add(12);filter过滤对象数组
list.add(13);
不指定⽬标数组数据类型获得的数组类型是Object类型:
List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(13);
2.使⽤toArray⽅法是出现java.lang.ArrayStoreException异常
public class StingUtilsTest{
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(13);
}
}
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.Array(ArrayList.java:390)
at common.lang.StingUtilsTest.main(StingUtilsTest.java:23)
分析:出现这种异常是由于数组中存⼊的数据与要转换的⽬标数组的类型不⼀致导致的;还有⼀点需要注意的是toArray参数数组的初始化⼤⼩如果list.size⼤于等于list的列表的长度那么就默认使⽤当前的参数数组,如果⼩于list的长度就会重新创建⼀个数组,建议如果知道list的长度⼀定要初始化数组的长度,这样可以节省内存空间,提⾼效率;
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。