首页 > 代码库 > 定义一个静态方法,该方法可以接收一个List<Integer>,方法内对List进行排序

定义一个静态方法,该方法可以接收一个List<Integer>,方法内对List进行排序

/**9定义一个静态方法,该方法可以接收一个List<Integer>,方法内对List进行排序*/

import java.util.ArrayList;

import java.util.List;

 

publicclass Test9 {

 

        publicstatic 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]);

                }

        }

 

        publicstatic void main(String[] args) {

 

                List<Integer> list =newArrayList<Integer>();

                for (int i = 9; i > 0; i--) {//产生数据

                        list.add(i);

                }

                System.out.println(list);

                Test9.sort(list);

                System.out.println(list);

 

        }

 

}