Java中ArrayList如何冒泡排序

  • Post author:
  • Post category:java


代码如下:随机生成一组随机数,并按照从小到大排列

import java.util.ArrayList;
import java.util.Random;

public class arrLIstRandom {
    public static void main(String[] args) {
        Random r = new Random();
        ArrayList<Integer> intList = new ArrayList<>();
        for (int i = 0; i < 33; i++) {
            int num = r.nextInt(33) + 1;
            intList.add(num);
        }
        System.out.println(intList);
//        ArrayList<Integer> newIntList = new ArrayList<>();
        for (int i = 0; i < intList.size() - 1; i++) {
            for (int i1 = 0; i1 < intList.size() - 1 - i; i1++) {
                if (intList.get(i1)>intList.get(i1+1)){
                    int temp = intList.get(i1);
                    intList.set(i1,intList.get(i1+1));
                    intList.set(i1+1,temp);
                }
            }
        }
        System.out.println(intList);
    }
}



版权声明:本文为songJunFeng1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。