您现在的位置是:主页 > news > 静态网页设计作业成品/超级seo外链工具

静态网页设计作业成品/超级seo外链工具

admin2025/5/4 17:04:22news

简介静态网页设计作业成品,超级seo外链工具,俄罗斯视频网站开发,沙坪坝做网站1.collection.sort()对integer泛型的list进行排序 /** 1.collection.sort()对integer泛型的list进行排序* 创建一个integer泛型的list&#xff0c;插入10个100以内不重复的随机整数* 调用colletions.sort()方法对其进行排序*/public void testSort1() {List<Integer> int…

静态网页设计作业成品,超级seo外链工具,俄罗斯视频网站开发,沙坪坝做网站1.collection.sort()对integer泛型的list进行排序 /** 1.collection.sort()对integer泛型的list进行排序* 创建一个integer泛型的list&#xff0c;插入10个100以内不重复的随机整数* 调用colletions.sort()方法对其进行排序*/public void testSort1() {List<Integer> int…

1.collection.sort()对integer泛型的list进行排序

 

/** 1.collection.sort()对integer泛型的list进行排序* 创建一个integer泛型的list,插入10个100以内不重复的随机整数* 调用colletions.sort()方法对其进行排序*/public void testSort1() {List<Integer> integerList = new ArrayList<>();//插入10个100以内不重复的随机整数Random random = new Random();Integer k;for (int i = 0; i < 10; i++) {do {k = random.nextInt(100);} while (integerList.contains(k));integerList.add(k);System.out.println("成功添加整数:"+k);}System.out.println("----------排序前------------");for (Integer integer : integerList) {System.out.println("元素:"+integer);}Collections.sort(integerList);System.out.println("----------排序后------------");for (Integer integer : integerList) {System.out.println("元素:"+integer);}}

2.对string泛型的list集合进行排序

 

排序规则:先按照首字母进行排序,如果第一个首字母相同,则按照第二位进行排序

                  排列顺序:0-9    A-Z    a-z

 

public void testSort2() {List<String> stringList = new ArrayList<>();stringList.add("microsoft");stringList.add("google");stringList.add("lenove");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);}}

3.对对象(student)类型泛型的list集合排序:

 

在java中,俩个对象排序就必须是可以比较的-----comparable接口。比较的标准不同就会产生不同的结果

 

          comparable  -----------默认的比较规则

          comparator  ------------临时的比较规则

 

/*** 3.对student对象类型进行排序*/public void testSort3() {List<Student> studentList = new  ArrayList<>();studentList.add(new Student(3,"","小明"));studentList.add(new Student(21,"","小王"));studentList.add(new Student(1,"","小张"));System.out.println("--------排序前--------");for (Student student : studentList) {System.out.println(student);}Collections.sort(studentList);System.out.println("--------排序后--------");for (Student student : studentList) {System.out.println(student);}}

要比较student实体类必须继承comparable接口,重写它的compareTo方法:

 

 

package it.com.test;public class Student implements Comparable<Student> {private Integer stuId;private String  subject;private String  studentName;public Student() {super();// TODO Auto-generated constructor stub}public Student(Integer stuId, String subject, String studentName) {super();this.stuId = stuId;this.subject = subject;this.studentName = studentName;}public Integer getStuId() {return stuId;}public void setStuId(Integer stuId) {this.stuId = stuId;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}@Overridepublic String toString() {return " [stuId=" + stuId + ", subject=" + subject + ", studentName=" + studentName + "]";}@Overridepublic int compareTo(Student arg0) {// 这里只是简单的按照单一属性比较,实际应用中往往要按照多个属性去重写compareTo方法return this.stuId.compareTo(arg0.stuId);}
}

结果如下:

 

上面展示了comparable默认按id排序,下面展示临时按名字排序

 

package it.com.test;import java.util.Comparator;public class StudentComparetor implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {// TODO Auto-generated method stubreturn o1.getStudentName().compareTo(o2.getStudentName());}}


实际代码如下:

 

 

/*** 3.对student对象类型进行排序*/public void testSort3() {List<Student> studentList = new  ArrayList<>();studentList.add(new Student(3,"","anglar"));studentList.add(new Student(21,"","beyounce"));studentList.add(new Student(1,"","Lucy"));studentList.add(new Student(4,"","Hucy"));System.out.println("--------排序前--------");for (Student student : studentList) {System.out.println(student);}Collections.sort(studentList);System.out.println("--------排序后--------");for (Student student : studentList) {System.out.println(student);}Collections.sort(studentList,new StudentComparetor());System.out.println("--------按照姓名排序后--------");for (Student student : studentList) {System.out.println(student);}}


结果展示:

 

 

 

//对list进行重新按照glxxlx进行升序-从小到大
if (null != list&& list.size()>0) {Collections.sort(list,new Comparator<Map>() {@Overridepublic int compare(Map o1, Map o2) {int ret = 0;//比较两个对象的顺序,如果前者小于、等于或者大于后者,则分别返回-1/0/1ret = o1.get("GLXXLX").toString().compareTo(o2.get("GLXXLX").toString());//逆序的话就用o2.compareTo(o1)即可return ret;}}); 
}