我是一个方法,它接受一个数字列表(例如 ArrayList
)并将其转换为排序集(例如 TreeSet
)并返回它。我写了代码,但我遇到了一些问题。
public TreeSet getSort (ArrayList list){
TreeSet set =new TreeSet(list);
return set;
我的问题主要在:
ArrayList List = new ArrayList();
List.add(5);
List.add(55);
List.add(88);
List.add(555);
List.add(154);
System.out.println("the TreeSet of ArrayList is : " + getSort(List));
最佳答案
你需要有类实例来调用getSort()
方法或者使getSort()
变成static
喜欢
System.out.println("the TreeSet of ArrayList is : "+ new classname().getSort(List));
或将方法设为静态
public static TreeSet getSort (ArrayList list){
TreeSet set =new TreeSet(list);
return set;
}
关于java - 将 ArrayList 转换为有序集(TreeSet)并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23137758/