ITPub博客

首页 > IT职业 > IT生活 > 通过Collections.sort()方法 * 对Integer泛型

通过Collections.sort()方法 * 对Integer泛型

IT生活 作者:ebrain68 时间:2021-09-09 09:29:04 0 删除 编辑
package com.imooc.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class CollectionTest {
    /**
     * 通过Collections.sort()方法,
     * 对Integer泛型的List,插入十个100以内的不重复的随机整数,
     * 调用Collections.sort()方法对其进行排序
     * @author qtw1998
     */
    public void testSort1() {

        List integerList = new ArrayList();
        //插入十个100以内的不重复的随机整数
        Random random = new Random();//随机整数
        Integer k;
        for(int i = 0;i  randomString(int length){
            List tempList = new ArrayList();
            String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            // 添加十条随机字符串
            for (int i = 0; i  stringList = new ArrayList();
        stringList.add("microsoft");
        stringList.add("google");
        stringList.add("lenovo");
        System.out.println("------排序前-------");
        for (String string : stringList) {
            System.out.println("元素:" + string);
        }
        Collections.sort(stringList);
        System.out.println("-------排序后--------");
        for (String string : stringList) {
            System.out.println("元素:" + string);
        }

    }
    /*
     * 利用Collections.sort()方法对泛型string的list版本进行排序的版本
     * 创建完list之后,往其中添加十条随机字符串
     * 每条字符串长度为10以内的随机整数
     * 每条字符串每个分字符都为随机生成的字符,字符可以重复
     * 每条随机字符串不可以重复
     */
    public void testsort3() {
        List stringList = randomString(10);
        int k = 0;
//      for(int i = 0;i 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2920/viewspace-2803496/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-13

  • 博文量
    68
  • 访问量
    450