将 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;
我的问题主要在于:
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屋!
查看全文