您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“java.lang.ArrayStoreException異常怎么解決”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“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)造。
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();
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)存空間,提高效率;
到此,相信大家對(duì)“java.lang.ArrayStoreException異常怎么解決”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。