欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java.lang.ArrayStoreException異常的解決方案

 更新時間:2021年12月30日 11:20:42   作者:我是蟻人  
這篇文章主要介紹了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.stream.AbstractPipeline.copyInto(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.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
at

查詢百度的解釋:試圖將錯誤類型的對象存儲到一個對象數(shù)組時拋出的異常。之后,在看看自己錯誤的代碼:

Field[] filterCopyFields = Stream.of(appendFields)
     .map(f -> !preFieldNames.contains(f.getName())).toArray(Field[]::new);

很容易看出問題的所在,這里我是想過濾Field[]數(shù)組中的元素,!preFieldNames.contains(f.getName())這個是過濾條件,發(fā)現(xiàn)了這里使用的居然是map,過濾應(yīng)該是使用filter,map中的元素應(yīng)該是返回結(jié)果并在toArray方法中轉(zhuǎn)換成數(shù)組,這里map中返回的是Boolean布爾類型的數(shù)據(jù),也就是說不能將boolean類型的對象存儲到Field對象數(shù)組中。

這里可以看一下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()的參數(shù)是IntFunction<A[]>類型,從@param A the element type of the resulting array這個注解中可以看到,A是表示返回數(shù)組的元素類型,在我的例子中返回類型是一個Field,而如果Stream中使用了map遍歷,返回的類型又是Boolean,類型不匹配而出現(xiàn)錯誤。

解決更改

Field[] filterCopyFields = Stream.of(appendFields)
     .filter(f -> !preFieldNames.contains(f.getName())).toArray(Field[]::new);

其實這種小問題應(yīng)該很容易避免,在出現(xiàn)ArrayStoreException異常時應(yīng)該對應(yīng)著數(shù)組中的元素類型去查找錯誤,構(gòu)造數(shù)組時應(yīng)按照正確的類型來構(gòu)造。

Java工具類List的toArray方法及java.lang.ArrayStoreException

1.List接口中有兩個方法

Object[] toArray(); 
T[] toArray(T[] a);

分析:不帶參數(shù)的方法默認是把數(shù)組轉(zhuǎn)換為Object類型,而帶參數(shù)的方法會將數(shù)組轉(zhuǎn)換為指定的類型;

指定目標數(shù)組數(shù)據(jù)類型:

        List<Integer> list = new ArrayList<Integer>();
        list.add(12);
        list.add(13);
        list.toArray(new Integer[list.size()]);

不指定目標數(shù)組數(shù)據(jù)類型獲得的數(shù)組類型是Object類型:

        List<Integer> list = new ArrayList<Integer>();
        list.add(12);
        list.add(13);
        list.toArray();

2.使用toArray方法是出現(xiàn)java.lang.ArrayStoreException異常

public class StingUtilsTest{
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(12);
        list.add(13);
        list.toArray(new Long[list.size()]);
    }
}

Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(ArrayList.java:390)
at common.lang.StingUtilsTest.main(StingUtilsTest.java:23)

分析:出現(xiàn)這種異常是由于數(shù)組中存入的數(shù)據(jù)與要轉(zhuǎn)換的目標數(shù)組的類型不一致導致的;還有一點需要注意的是toArray參數(shù)數(shù)組的初始化大小如果list.size大于等于list的列表的長度那么就默認使用當前的參數(shù)數(shù)組,如果小于list的長度就會重新創(chuàng)建一個數(shù)組,建議如果知道list的長度一定要初始化數(shù)組的長度,這樣可以節(jié)省內(nèi)存空間,提高效率;

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論