collection.clear(); //clear the collection before adding the variables
collection.add(variable); //add the variables
collection.add(variable1);
collection.add(variable2);
Collections.sort(collection);//sorts the collection from smaller to larger
Collections.reverse(collection);//you can use this code in case you want to sort from larger to smaller instead
1 回答
例如,有3个变量叫做variable1,variable2和variable3 . 让我们添加一个带有double类型元素的集合(arrayList)(假设你的变量是double类型)
每次要对这些值进行排序时:
然后,您可以通过执行来访问该集合
它在哪里是集合的索引(在这种情况下有3个变量,它可以是0,1或2)