将java ArrayList <comparative>转换为Comparable [] </comparative>

时间:2013-03-06 07:40:57

标签: java arraylist comparable

我正在尝试将一些Comparable元素放入集合中并将其作为Comparable []返回。

public Comparable[] getPerms() {
    ArrayList<Comparable> resList = new ArrayList<Comparable>();
    while(hasNext()) {
       for (int i=0; i<r; i++) {
         resList.add(index[i]);
       }
       moveIndex();
    }
    Comparable[] resultArray ;//how convert resList into array
    return resultArray;
  }

但是,我不确定如何将ArrayList<Comparable>作为Comparable[]

任何帮助表示感谢。

3 个答案:

答案 0 :(得分:2)

resList.toArray(new Comparable[resList.size()])

答案 1 :(得分:2)

怎么样?
    resList.toArray(new Comparable[resList.size()]);

答案 2 :(得分:1)

您需要使用ArrayList#toArray()方法。
resList.toArray(new Comparable[resList.size()])可以解决您的问题。有关详细信息please see this

相关问题