-2
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Comparable
at java.util.Collections.sort(Collections.java:121)
at day3.collections.SortArrayList.SortArrayList.main(SortArrayList.java:20)

package day3.collections.arraylist;

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListMain {

private ArrayList a;

public void demo(){

    try{

        System.out.println("enter the no to add in the array list");
        Scanner in = new Scanner(System.in);

        int digitno = in.nextInt();
        int numbers[] = new int[digitno];

        Scanner sc=new Scanner(System.in);
        System.out.println("Please enter numbers...");
        ArrayList arrayList1 = new ArrayList();



    for(
            int j=0;j<digitno;j++){
            numbers[j]=sc.nextInt();
            arrayList1.add(numbers[j]);
            setA(arrayList1);
        }

   }
    catch(IndexOutOfBoundsException e){
        System.out.println("Error:  Entered number is not found");
    }

   catch(Exception e){
       System.out.println("Error:  Please enter integer values only");

   }

}

public ArrayList getA() {
    return a;
}

public void setA(ArrayList a) {
    this.a = a;
}

}



package day3.collections.SortArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import day3.collections.arraylist.ArrayListMain;

public class SortArrayList extends ArrayListMain {

 public static void main(String[] args) {


  ArrayListMain arrayList=new ArrayListMain();
    ArrayList arrayL=new ArrayList();
    arrayList.demo();
    arrayL.add(arrayList.getA());


Collections.sort(arrayL);


System.out.println("ArrayList elements after sorting in ascending order : ");
for(int i=0; i<arrayL.size(); i++)
  System.out.println(arrayL.get(i));

}
}
4

4 回答 4

1

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 ).

于 2013-11-15T11:26:58.817 回答
0

Try to make the arraL a List and add all elements of your arrayList. So:

List<WhateverTypeYouNeed> arrayL = new ArrayList<WhateverTypeYouNeed>();
arrayL.addAll(arrayList.getA());
Collections.sort(arrayL);
于 2013-11-15T11:26:49.307 回答
0

You are putting an instance of ArrayList into another ArrayList and then sort the latter. Sorting this way requires all elements to be Comparable, but as an ArrayList is not Comparable, your code fails.

It think the problem is in line:

arrayL.add(arrayList.getA());

It probably should be:

arrayL.addAll(arrayList.getA());

Btw.: Horrible formatting and naming! Consider redesign!

于 2013-11-15T11:26:56.773 回答
0

For the fact that ArrayList does not implement Comparable you will have to use the sort()-method that takes a Comparator-object as parameter, and implement a compare-method yourself: http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)

于 2013-11-15T11:32:27.697 回答