In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to solve java.lang.ArrayStoreException anomalies". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve java.lang.ArrayStoreException anomalies.
Java.lang.ArrayStoreException exception hint
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
Query Baidu's explanation: the exception thrown when trying to store an object of the wrong type into an array of objects. After that, look at your own wrong code:
Field [] filterCopyFields = Stream.of (appendFields) .map (f->! preFieldNames.contains (f.getName () .toArray (Field []:: new)
It's easy to see where the problem lies. Here I want to filter the elements in the Field [] array.! preFieldNames.contains (f.getName ()) this is the filter condition. I found that the filter is actually using map. The filter should use the elements in filter,map to return the results and convert them into arrays in the toArray method. Here map returns data of Boolean Boolean type. That is, objects of type boolean cannot be stored in an array of Field objects.
You can take a look at the definition of the toArray (IntFunction generator) method in the JDK8 source code:
/ * * 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. * *
This is a terminal * operation. * * @ 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: * {@ code * Person [] men = people.stream () * .filter (p-> p.getGender () = = MALE) * .toArray (Person []:: new) * * @ param 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 [] toArray (IntFunction generator)
You can see that the argument to toArray () is of type IntFunction, and you can see from the @ param A the element type of the resulting array annotation that An is the element type that returns the array, in my case the return type is a Field, and if map traversal is used in Stream, the returned type is Boolean, and the type does not match and an error occurs.
Resolve changing Field [] filterCopyFields = Stream.of (appendFields) .filter (f->! preFieldNames.contains (f.getName () .toArray (Field []:: new)
In fact, this small problem should be easy to avoid. When an ArrayStoreException exception occurs, the error should be found corresponding to the element type in the array, and the array should be constructed according to the correct type.
In the toArray method and java.lang.ArrayStoreException1.List interface of the Java utility class List, there are two methods Object [] toArray (); T [] toArray (T [] a)
Parsing: the default method without parameters is to convert the array to the Object type, while the method with parameters converts the array to the specified type
Specify the target array data type:
List list = new ArrayList (); list.add (12); list.add (13); list.toArray (new Integer [list.size ()])
The array type obtained without specifying the target array data type is the Object type:
List list = new ArrayList (); list.add (12); list.add (13); list.toArray (); 2. Using the toArray method, there is a java.lang.ArrayStoreException exception public class StingUtilsTest {public static void main (String [] args) {List list = new ArrayList (); 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)
Analysis: this exception occurs because the data stored in the array is inconsistent with the type of the target array to be converted It is also important to note that the initialization size of the toArray parameter array if list.size is greater than or equal to the length of the list of list, then the current parameter array is used by default, and if it is less than the length of list, a new array will be created. It is recommended that if you know the length of list, you must initialize the length of the array, which can save memory space and improve efficiency.
At this point, I believe you have a deeper understanding of "how to solve java.lang.ArrayStoreException anomalies". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.