System.arraycopy getting java.lang.ArrayIndexOutOfBoundsException

System.arraycopy getting java.lang.ArrayIndexOutOfBoundsException



System.arraycopy getting ava.lang.ArrayIndexOutOfBoundsException.. I am trying to copy data from one array to the next. but I am getting a exception


private String array = "NO DATA YET" ;


private void setListData()

String array2 = "Iphone", "Tutorials", "Gallery", "Android", "item 1", "item 2", "item3", "item 4" ;
System.arraycopy(array2, 0, array, 0, array2.length);




4 Answers
4



You're trying to copy 8 items into an array of length 1. You can't do that.



From the documentation:



Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified:



In this case, destPos + length is 8, and dest.length is 1, hence the exception is being thrown.


destPos + length


dest.length



Note that arrays in Java have a fixed length. If you want an expandable container, look at ArrayList.


ArrayList






Thank you sir @Jon Skeet, for this documentation answer

– Kushal
Apr 24 '15 at 12:56






The doc says, it will throw IndexOutOfBoundsException, however, it threw ArrayIndexOutOfBoundsException. That's what confused me, because in my algorithm, I cannot figure out, what was wrong, as it does not provide any details.

– Oliv
Feb 6 '17 at 5:23



IndexOutOfBoundsException


ArrayIndexOutOfBoundsException






@Oliv: Well an ArrayIndexOutOfBoundsException is an IndexOutOfBoundsException - it's a subclass. If you know which statement in your method is failing, you can add logging just before it for what your call is going to do - and then it should be obvious which parameter is causing the problem.

– Jon Skeet
Feb 6 '17 at 6:36



ArrayIndexOutOfBoundsException


IndexOutOfBoundsException



Because the length of array is 1.


array



The declaration array = "NO DATA YET" ; creates an array of length 1 with one item in it.


array = "NO DATA YET" ;



Instead declare destination array as:


private String array = new String[8];



array only has a length of 1 and you're trying to copy 8 things into it from array2. Try using an ArrayList or something that will grow as you need it to grow.



SOLUTION TO ALL TYPES OF ARRAYINDEXOUTOFBOUNDS EXCEPTION:



Understand this with the help of following example facing ArrayIndexOutOfBoundsException:


public static void main(String args)
String dest = "Iphone", "Tutorials", "Gallery", "Android", "item 1", "item 2", "item3", "item 4" ;
String src=new String[8];
System.arraycopy(src, 0, dest, 1, src.length);
System.out.println(dest[4]);



Solution to this above case can be any of the following:
1. Either increase the size of destination array so it could find a place.
2. Since we are starting from index 1 so you may copy only 7 elements from the source array.
3. Or you can start copying from index 0 in the destination array so it can accommodate all the array elements of source array.



Thanks for contributing an answer to Stack Overflow!



But avoid



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



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)