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
查詢(xún)百度的解釋?zhuān)涸噲D將錯(cuò)誤類(lèi)型的對(duì)象存儲(chǔ)到一個(gè)對(duì)象數(shù)組時(shí)拋出的異常。之后,在看看自己錯(cuò)誤的代碼:
Field[] filterCopyFields = Stream.of(appendFields) .map(f -> !preFieldNames.contains(f.getName())).toArray(Field[]::new);
很容易看出問(wèn)題的所在,這里我是想過(guò)濾Field[]數(shù)組中的元素,!preFieldNames.contains(f.getName())這個(gè)是過(guò)濾條件,發(fā)現(xiàn)了這里使用的居然是map,過(guò)濾應(yīng)該是使用filter,map中的元素應(yīng)該是返回結(jié)果并在toArray方法中轉(zhuǎn)換成數(shù)組,這里map中返回的是Boolean布爾類(lèi)型的數(shù)據(jù),也就是說(shuō)不能將boolean類(lèi)型的對(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[]>類(lèi)型,從@param A the element type of the resulting array這個(gè)注解中可以看到,A是表示返回?cái)?shù)組的元素類(lèi)型,在我的例子中返回類(lèi)型是一個(gè)Field,而如果Stream中使用了map遍歷,返回的類(lèi)型又是Boolean,類(lèi)型不匹配而出現(xiàn)錯(cuò)誤。
解決更改
Field[] filterCopyFields = Stream.of(appendFields) .filter(f -> !preFieldNames.contains(f.getName())).toArray(Field[]::new);
其實(shí)這種小問(wèn)題應(yīng)該很容易避免,在出現(xiàn)ArrayStoreException異常時(shí)應(yīng)該對(duì)應(yīng)著數(shù)組中的元素類(lèi)型去查找錯(cuò)誤,構(gòu)造數(shù)組時(shí)應(yīng)按照正確的類(lèi)型來(lái)構(gòu)造。
Java工具類(lèi)List的toArray方法及java.lang.ArrayStoreException
1.List接口中有兩個(gè)方法
Object[] toArray(); T[] toArray(T[] a);
分析:不帶參數(shù)的方法默認(rèn)是把數(shù)組轉(zhuǎn)換為Object類(lèi)型,而帶參數(shù)的方法會(huì)將數(shù)組轉(zhuǎn)換為指定的類(lèi)型;
指定目標(biāo)數(shù)組數(shù)據(jù)類(lèi)型:
List<Integer> list = new ArrayList<Integer>(); list.add(12); list.add(13); list.toArray(new Integer[list.size()]);
不指定目標(biāo)數(shù)組數(shù)據(jù)類(lèi)型獲得的數(shù)組類(lèi)型是Object類(lèi)型:
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ù)組的類(lèi)型不一致導(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)文章
springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過(guò)程
這篇文章主要給大家介紹了關(guān)于springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過(guò)程,Spring Boot應(yīng)用中可以配置多個(gè)數(shù)據(jù)源,并根據(jù)注解靈活指定當(dāng)前使用的數(shù)據(jù)源,需要的朋友可以參考下2023-08-08Java如何將若干時(shí)間區(qū)間進(jìn)行合并的方法步驟
這篇文章主要介紹了Java如何將若干時(shí)間區(qū)間進(jìn)行合并的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Java使用PrepareStatement實(shí)現(xiàn)數(shù)據(jù)的插入與查詢(xún)操作
這篇文章主要為大家詳細(xì)介紹了Java如何使用PrepareStatement實(shí)現(xiàn)數(shù)據(jù)的插入與查詢(xún)操作,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09Spring Boot Jar 包部署腳本的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于Spring Boot Jar 包部署腳本的實(shí)例講解內(nèi)容,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-12-12Kotlin 語(yǔ)言中調(diào)用 JavaScript 方法實(shí)例詳解
這篇文章主要介紹了Kotlin 語(yǔ)言中調(diào)用 JavaScript 方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Spring?Boot在啟動(dòng)時(shí)執(zhí)行一次的功能實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Spring?Boot在啟動(dòng)時(shí)執(zhí)行一次的功能實(shí)現(xiàn),在實(shí)習(xí)過(guò)程中,有時(shí)候會(huì)遇到一些項(xiàng)目啟動(dòng)初始化的需求,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08迅速學(xué)會(huì)@ConfigurationProperties的使用操作
這篇文章主要介紹了迅速學(xué)會(huì)@ConfigurationProperties的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java簡(jiǎn)易抽獎(jiǎng)系統(tǒng)小項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Java簡(jiǎn)易抽獎(jiǎng)系統(tǒng)小項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Javaweb實(shí)現(xiàn)完整個(gè)人博客系統(tǒng)流程
這篇文章主要介紹了怎樣用Java來(lái)實(shí)現(xiàn)一個(gè)完整的個(gè)人博客系統(tǒng),我們通過(guò)實(shí)操上手的方式可以高效的鞏固所學(xué)的基礎(chǔ)知識(shí),感興趣的朋友一起來(lái)看看吧2022-03-03運(yùn)行時(shí)常量池和字符串常量池的區(qū)別及說(shuō)明
這篇文章主要介紹了運(yùn)行時(shí)常量池和字符串常量池的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12