java.lang.ArrayStoreException storing Integer in Integer array The Next CEO of Stack OverflowCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?How do I generate random integers within a specific range in Java?How do I determine whether an array contains a particular value in Java?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?Why is it faster to process a sorted array than an unsorted array?

Method for adding error messages to a dictionary given a key

Make solar eclipses exceedingly rare, but still have new moons

Which one is the true statement?

TikZ: How to reverse arrow direction without switching start/end point?

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

How to write a definition with variants?

Break Away Valves for Launch

Reference request: Grassmannian and Plucker coordinates in type B, C, D

A small doubt about the dominated convergence theorem

What connection does MS Office have to Netscape Navigator?

Proper way to express "He disappeared them"

Flying from Cape Town to England and return to another province

Why do remote US companies require working in the US?

Why doesn't UK go for the same deal Japan has with EU to resolve Brexit?

Can this equation be simplified further?

Why is information "lost" when it got into a black hole?

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?

How to invert MapIndexed on a ragged structure? How to construct a tree from rules?

What steps are necessary to read a Modern SSD in Medieval Europe?

What was the first Unix version to run on a microcomputer?

Is there a difference between "Fahrstuhl" and "Aufzug"

How to check if all elements of 1 list are in the *same quantity* and in any order, in the list2?

Is micro rebar a better way to reinforce concrete than rebar?

Writing differences on a blackboard



java.lang.ArrayStoreException storing Integer in Integer array



The Next CEO of Stack OverflowCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?How do I generate random integers within a specific range in Java?How do I determine whether an array contains a particular value in Java?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?Why is it faster to process a sorted array than an unsorted array?










0















Why am I getting this exception?



java.lang.ArrayStoreException: java.lang.Integer
at MyLinkedList.toArray(MyLinkedList.java:94)
at MyLinkedListTest.toArray_ReturnGenericArray(MyLinkedListTest.java:80)


I am creating an Integer array and passing in Integer values. Why then, when I create a new instance of the same type of array, am I unable to assign integer values to it?



@Override
public <T1> T1[] toArray(T1[] a)
if (a.length < size)
a = (T1[]) java.lang.reflect.Array.newInstance(a.getClass(), size);

Node<T> current = head;
int i = 0;
Object[] result = a;
while (current != null)
result[i] = current.value;
i++;
current = current.next;

// if array has room to spare set element immediately following end of list to null
if (a.length > i)
a[i] = null;

return a;


@Test
void toArray_ReturnGenericArray()
Integer[] array2 = linkedList.toArray(new Integer[4]);
assertEquals(1, array2[0]);
assertEquals(2, array2[1]);
assertEquals(3, array2[2]);
assertEquals(4, array2[3]);
assertEquals(5, array2[4]);
assertEquals(5, array2.length);










share|improve this question






















  • Use java.util.Arrays#copyOf(U[], int, java.lang.Class<? extends T[]>)

    – luk2302
    Mar 7 at 16:57















0















Why am I getting this exception?



java.lang.ArrayStoreException: java.lang.Integer
at MyLinkedList.toArray(MyLinkedList.java:94)
at MyLinkedListTest.toArray_ReturnGenericArray(MyLinkedListTest.java:80)


I am creating an Integer array and passing in Integer values. Why then, when I create a new instance of the same type of array, am I unable to assign integer values to it?



@Override
public <T1> T1[] toArray(T1[] a)
if (a.length < size)
a = (T1[]) java.lang.reflect.Array.newInstance(a.getClass(), size);

Node<T> current = head;
int i = 0;
Object[] result = a;
while (current != null)
result[i] = current.value;
i++;
current = current.next;

// if array has room to spare set element immediately following end of list to null
if (a.length > i)
a[i] = null;

return a;


@Test
void toArray_ReturnGenericArray()
Integer[] array2 = linkedList.toArray(new Integer[4]);
assertEquals(1, array2[0]);
assertEquals(2, array2[1]);
assertEquals(3, array2[2]);
assertEquals(4, array2[3]);
assertEquals(5, array2[4]);
assertEquals(5, array2.length);










share|improve this question






















  • Use java.util.Arrays#copyOf(U[], int, java.lang.Class<? extends T[]>)

    – luk2302
    Mar 7 at 16:57













0












0








0








Why am I getting this exception?



java.lang.ArrayStoreException: java.lang.Integer
at MyLinkedList.toArray(MyLinkedList.java:94)
at MyLinkedListTest.toArray_ReturnGenericArray(MyLinkedListTest.java:80)


I am creating an Integer array and passing in Integer values. Why then, when I create a new instance of the same type of array, am I unable to assign integer values to it?



@Override
public <T1> T1[] toArray(T1[] a)
if (a.length < size)
a = (T1[]) java.lang.reflect.Array.newInstance(a.getClass(), size);

Node<T> current = head;
int i = 0;
Object[] result = a;
while (current != null)
result[i] = current.value;
i++;
current = current.next;

// if array has room to spare set element immediately following end of list to null
if (a.length > i)
a[i] = null;

return a;


@Test
void toArray_ReturnGenericArray()
Integer[] array2 = linkedList.toArray(new Integer[4]);
assertEquals(1, array2[0]);
assertEquals(2, array2[1]);
assertEquals(3, array2[2]);
assertEquals(4, array2[3]);
assertEquals(5, array2[4]);
assertEquals(5, array2.length);










share|improve this question














Why am I getting this exception?



java.lang.ArrayStoreException: java.lang.Integer
at MyLinkedList.toArray(MyLinkedList.java:94)
at MyLinkedListTest.toArray_ReturnGenericArray(MyLinkedListTest.java:80)


I am creating an Integer array and passing in Integer values. Why then, when I create a new instance of the same type of array, am I unable to assign integer values to it?



@Override
public <T1> T1[] toArray(T1[] a)
if (a.length < size)
a = (T1[]) java.lang.reflect.Array.newInstance(a.getClass(), size);

Node<T> current = head;
int i = 0;
Object[] result = a;
while (current != null)
result[i] = current.value;
i++;
current = current.next;

// if array has room to spare set element immediately following end of list to null
if (a.length > i)
a[i] = null;

return a;


@Test
void toArray_ReturnGenericArray()
Integer[] array2 = linkedList.toArray(new Integer[4]);
assertEquals(1, array2[0]);
assertEquals(2, array2[1]);
assertEquals(3, array2[2]);
assertEquals(4, array2[3]);
assertEquals(5, array2[4]);
assertEquals(5, array2.length);







java arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 16:50









Co CodesCo Codes

31




31












  • Use java.util.Arrays#copyOf(U[], int, java.lang.Class<? extends T[]>)

    – luk2302
    Mar 7 at 16:57

















  • Use java.util.Arrays#copyOf(U[], int, java.lang.Class<? extends T[]>)

    – luk2302
    Mar 7 at 16:57
















Use java.util.Arrays#copyOf(U[], int, java.lang.Class<? extends T[]>)

– luk2302
Mar 7 at 16:57





Use java.util.Arrays#copyOf(U[], int, java.lang.Class<? extends T[]>)

– luk2302
Mar 7 at 16:57












1 Answer
1






active

oldest

votes


















1














The main issue is this bit of code



a.getClass()


What it will return is not the class of the component of the array, but the array itself, e.g.



[Ljava.lang.Integer


See the [L prefix. You need to use



a.getClass().getComponentType()


As Array#newInstance accepts the component type



newInstance(Class<?> componentType, int length)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55048988%2fjava-lang-arraystoreexception-storing-integer-in-integer-array%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The main issue is this bit of code



    a.getClass()


    What it will return is not the class of the component of the array, but the array itself, e.g.



    [Ljava.lang.Integer


    See the [L prefix. You need to use



    a.getClass().getComponentType()


    As Array#newInstance accepts the component type



    newInstance(Class<?> componentType, int length)





    share|improve this answer



























      1














      The main issue is this bit of code



      a.getClass()


      What it will return is not the class of the component of the array, but the array itself, e.g.



      [Ljava.lang.Integer


      See the [L prefix. You need to use



      a.getClass().getComponentType()


      As Array#newInstance accepts the component type



      newInstance(Class<?> componentType, int length)





      share|improve this answer

























        1












        1








        1







        The main issue is this bit of code



        a.getClass()


        What it will return is not the class of the component of the array, but the array itself, e.g.



        [Ljava.lang.Integer


        See the [L prefix. You need to use



        a.getClass().getComponentType()


        As Array#newInstance accepts the component type



        newInstance(Class<?> componentType, int length)





        share|improve this answer













        The main issue is this bit of code



        a.getClass()


        What it will return is not the class of the component of the array, but the array itself, e.g.



        [Ljava.lang.Integer


        See the [L prefix. You need to use



        a.getClass().getComponentType()


        As Array#newInstance accepts the component type



        newInstance(Class<?> componentType, int length)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 17:01









        LppEddLppEdd

        9,09621647




        9,09621647





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55048988%2fjava-lang-arraystoreexception-storing-integer-in-integer-array%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved