tags:

views:

128

answers:

2

How can I copy a CharArrayBuffer to another CharArrayBuffer at a specified index in Java on android?

developer.android.com/reference/android/…

CharArrayBuffer stringBuffer1 = new CharArrayBuffer(128); 
System.arraycopy("Phone", 0, stringBuffer1.data, 0, "Phone".length());

But i get an ArrayStoreException. I have allocated the CharArrayBuffer to be 128. I don't understand this exception

A: 

ArrayStoreException is because of a type. Convert "Phone" into a CharArrayBuffer. (The size of the array is not the issue. It will throw a IndexOutOfBoundsException if it is.)

Stu Thompson
+1  A: 

from the docs

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

ArrayStoreException Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException:

 Object x[] = new String[3];
 x[0] = new Integer(0);

following line of code

System.arraycopy("Phone", 0, stringBuffer1.data, 0, "Phone".length());

is trying to put String into CharArrayBuffer.

prateek urmaliya