The specifics as to why you are getting your problem is this:
- New ArrayList is created, empty
- All elements are typed to String in y
- All elements are typed to StringBuffer in z
- A new element is added to this array list.
- The JVM attempts to cast it to BOTH String and StringBuffer
- Because "Strings" is not an instance of StringBuffer, ClassCastException is thrown.
To avoid this, do not have an array typed to both String and StringBuffer. Select only one. For example:
ArrayList<StringBuffer> myArrayList = new ArrayList<StringBuffer>
If you want to copy a String arraylist into a StringBuffer ArrayList, you'll have to iterate over all of the items in z to convert them to string buffers:
ArrayList x=new ArrayList();
ArrayList<String>y=x;
ArrayList<StringBuffer>z=new ArrayList<StringBuffer>();
for(String s : y)
z.add(new StringBuffer(s));
The reason for this is that StringBuffer does not extent String. What's causing a problem is that you cannot cast a string, say "str" to a StringBuffer i.e. you cannot do StringBuffer s = (StringBuffer)"str";
.
To cast ArrayList x
to ArrayList<T>
, all elements of x
must be extend or be an instance of T
.
In addition, you should always use generic types, when possible. If you don't know what will be (for example) in the list, use:
ArrayList<?> x = new ArrayList<?>();
If you know a little bit more, such as that it will be a subclass of another class, use
ArrayList<? extends T>
or ArrayList<? implements T>
. This will help you avoid future ClassCastExceptions.