weixin_慕码人2241264
2022-04-10 22:15
import java.sql.SQLOutput; import java.util.*; public class TestCollections { public static String getRandomString(Integer length){ if(length==null || length <=0){ return null; } String base = "0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for(int i=0; i<length; i++){ //62个字符 int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } public void test4(){ List<Student> studentList=new ArrayList<Student>(); List<String> strings=new ArrayList<String>(); Random random=new Random(); String str; for (int i=0;i<3;i++) { do { str=getRandomString(3); }while(strings.contains(str)); strings.add(str); } studentList.add(new Student(strings.get(0),"make")); studentList.add(new Student(strings.get(1),"lucy")); studentList.add(new Student(strings.get(2),"jeke")); System.out.println("------排序前--------"); for (Student student:studentList){ System.out.println("学生编号:"+student.Id+","+"学生姓名:"+student.Name); } Collections.sort(studentList); System.out.println("-------排序后---------"); for (Student student:studentList){ System.out.println("学生编号:"+student.Id+","+"学生姓名:"+student.Name); } } public static void main(String[] args) { TestCollections testCollections=new TestCollections(); testCollections.test4(); } }
还没有人回答问题,可以看看其他问题
Java入门第三季
409787 学习 · 4340 问题
相似问题