将ArrayList转换为排序集(TreeSet)并将其返回 [英] Converts ArrayList into a sorted set (TreeSet) and returns it
本文介绍了将ArrayList转换为排序集(TreeSet)并将其返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是一种方法,该方法接受一个数字列表(例如ArrayList
)并将其转换为排序的集合(例如TreeSet
)并返回它.我写了代码,但是遇到了一些问题.
I a method that takes a list of numbers (e.g. ArrayList
) and converts it into a sorted set (e.g. TreeSet
) and returns it. I wrote code, but I'm having some problems.
public TreeSet getSort (ArrayList list){
TreeSet set =new TreeSet(list);
return set;
我的问题出在主要方面:
My problem is in main:
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
You need to have the class instance to call the getSort()
method or make the getSort()
to 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;
}
这篇关于将ArrayList转换为排序集(TreeSet)并将其返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文