arrayL.add(arrayList.getA());
should be arrayL.addAll(arrayList.getA());
Change your code like below
ArrayListMain arrayList=new ArrayListMain();
ArrayList arrayL=new ArrayList();
arrayList.demo();
arrayL.addAll(arrayList.getA());
^___here is the change
ArrayList not implemented the Comparable interface.
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
}
Collections.sort(); will sort based on the compareTo method of Comparable intefface.
public interface Comparable<T> {
public int compareTo(T o);
}
See the String class syntax and it implemented the Comparable interface.
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
{
public int compareTo(String anotherString) {
}
}
arrayL.add(arrayList.getA());
is equal to
ArrayList<String> a= new ArrayList<String>();
a.add("A");
a.add("B");
a.add("C");
a.add("D");
arrayL.add(a);
arrayL contains one arrayList object and its not implemented the Comparable interface so it could not sort(adding arrayList itself to arrayL).
arrayL.addAll(arrayList.getA());
is equal to
ArrayList<String> a= new ArrayList<String>();
a.add("A");
a.add("B");
a.add("C");
a.add("D");
arrayL.addAll(a);
arrayL contains one 4 String Objects and its implemented the Comparable interface so it able to sort(adding arrayList values to arrayL ).