我给你写了一个,写得不简洁,方便你看。Integer是类,是对象,int只是数据类型
PS:你可以自行修改成你想要的类型
- import java.util.ArrayList;
- import java.util.List;
- public class ListSort {
- public static void sort(List<Integer> list) {
- int size = list.size();
- int[] store = new int[size];
- for (int i = 0; i < size; i++) {
- store[i] = list.get(i);
- }
- for (int i = 0; i < size; i++) {
- for (int j = i; j < size; j++) {
- if (store[i] > store[j]) {
- int temp = store[j];
- store[j] = store[i];
- store[i] = temp;
- }
- }
- }
- for(int i = 0 ; i < size ; i++)
- {
- list.set(i, store[i]);
- }
- }
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<Integer>();
- for (int i = 9; i > 0; i--) {
- list.add(i);
- }
- System.out.println(list);
- ListSort.sort(list);
- System.out.println(list);
- }
- }
复制代码 |