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
查詢百度的解釋:試圖將錯(cuò)誤類型的對(duì)象存儲(chǔ)到一個(gè)對(duì)象數(shù)組時(shí)拋出的異常。之后,在看看自己錯(cuò)誤的代碼:
Field[] filterCopyFields = Stream.of(appendFields)
.map(f -> !preFieldNames.contains(f.getName())).toArray(Field[]::new);
很容易看出問題的所在,這里我是想過濾Field[]數(shù)組中的元素,!preFieldNames.contains(f.getName())這個(gè)是過濾條件,發(fā)現(xiàn)了這里使用的居然是map,過濾應(yīng)該是使用filter,map中的元素應(yīng)該是返回結(jié)果并在toArray方法中轉(zhuǎn)換成數(shù)組,這里map中返回的是Boolean布爾類型的數(shù)據(jù),也就是說不能將boolean類型的對(duì)象存儲(chǔ)到Field對(duì)象數(shù)組中。
這里可以看一下JDK8源碼中對(duì)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這個(gè)注解中可以看到,A是表示返回?cái)?shù)組的元素類型,在我的例子中返回類型是一個(gè)Field,而如果Stream中使用了map遍歷,返回的類型又是Boolean,類型不匹配而出現(xiàn)錯(cuò)誤。
解決更改
Field[] filterCopyFields = Stream.of(appendFields)
.filter(f -> !preFieldNames.contains(f.getName())).toArray(Field[]::new);
其實(shí)這種小問題應(yīng)該很容易避免,在出現(xiàn)ArrayStoreException異常時(shí)應(yīng)該對(duì)應(yīng)著數(shù)組中的元素類型去查找錯(cuò)誤,構(gòu)造數(shù)組時(shí)應(yīng)按照正確的類型來構(gòu)造。
Java工具類List的toArray方法及java.lang.ArrayStoreException
1.List接口中有兩個(gè)方法
Object[] toArray(); T[] toArray(T[] a);
分析:不帶參數(shù)的方法默認(rèn)是把數(shù)組轉(zhuǎn)換為Object類型,而帶參數(shù)的方法會(huì)將數(shù)組轉(zhuǎn)換為指定的類型;
指定目標(biāo)數(shù)組數(shù)據(jù)類型:
List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(13);
list.toArray(new Integer[list.size()]);
不指定目標(biāo)數(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)換的目標(biāo)數(shù)組的類型不一致導(dǎo)致的;還有一點(diǎn)需要注意的是toArray參數(shù)數(shù)組的初始化大小如果list.size大于等于list的列表的長(zhǎng)度那么就默認(rèn)使用當(dāng)前的參數(shù)數(shù)組,如果小于list的長(zhǎng)度就會(huì)重新創(chuàng)建一個(gè)數(shù)組,建議如果知道list的長(zhǎng)度一定要初始化數(shù)組的長(zhǎng)度,這樣可以節(jié)省內(nèi)存空間,提高效率;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java代碼規(guī)范與質(zhì)量檢測(cè)插件SonarLint的使用
本文主要介紹了Java代碼規(guī)范與質(zhì)量檢測(cè)插件SonarLint的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java獲取Process子進(jìn)程進(jìn)程ID方法詳解
這篇文章主要介紹了Java獲取Process子進(jìn)程進(jìn)程ID方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
使用SpringBoot編寫一個(gè)優(yōu)雅的單元測(cè)試
這篇文章主要為大家詳細(xì)介紹了如何使用SpringBoot編寫一個(gè)優(yōu)雅的單元測(cè)試,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-07-07
創(chuàng)建Jersey REST 服務(wù),基于Maven的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄獎(jiǎng)?chuàng)建Jersey REST 服務(wù),基于Maven的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

