java Collections,Arrays工具类集合
程序员文章站
2024-01-14 13:36:52
...
Collections工具类集合
- void shuffle(List<?> list)
打乱集合顺序 - void sort(List<T> list)
集合排序
demo:
public class CollectionsDemo {
public static void main(String[] args){
List<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
intList.add(3);
intList.add(4);
intList.add(5);
Collections.shuffle(intList);
for (Integer integer : intList) {
System.out.println(integer);
}
Collections.sort(intList);
for (Integer integer : intList) {
System.out.println(integer);
}
}
}
Arrays工具类集合
- 数组转List asList();
- 排序 sort();
- 转换为字符串toString();
public void arrayDemo(){
int[] nums = {1,2,3,4,5,6,7,8,9,0};
List<Integer> intList = Arrays.asList(1,2,3,4,5,6,7,8,9,0);
Arrays.sort(nums);
for (int num : nums) {
System.out.println(num);
}
String str = Arrays.toString(nums);
System.out.println(str);
}
转载于:https://www.jianshu.com/p/d4fe717d9de3