当前位置: 首页 > wzjs >正文

怎样做好外贸网站推广百度统计app下载

怎样做好外贸网站推广,百度统计app下载,网站运营维护方案,内江市规划建设教育培训中心网站一、Collections类 java.util.Collections 类是 Java 集合框架中的一个工具类,提供了一系列静态方法来操作和处理各种类型的集合。这些方法简化了对集合进行排序、查找、同步控制、创建只读集合等常见操作的过程。 二、常用方法 方法类别‌‌方法签名‌‌功能‌‌需…

一、Collections类

     java.util.Collections 类是 Java 集合框架中的一个工具类,提供了一系列静态方法来操作和处理各种类型的集合。这些方法简化了对集合进行排序、查找、同步控制、创建只读集合等常见操作的过程。

二、常用方法

方法类别方法签名功能需求/约束算法/返回值
排序sort(List<T> list)对列表元素按自然顺序升序排序元素必须实现 Comparable 接口归并排序或 TimSort
sort(List<T> list, Comparator<? super T> c)按自定义比较器排序需提供 Comparator 对象同上
查找binarySearch(List<? extends Comparable<? super T>> list, T key)二分查找元素列表必须已按自然顺序排序找到返回索引;未找到返回 -(插入点) - 1
binarySearch(List<? extends T> list, T key, Comparator<? super T> c)使用自定义比较器二分查找列表必须按比较器规则排序同上
最大值/最小值max(Collection<? extends T> coll)返回集合中的最大元素元素实现 Comparable 接口-
min(Collection<? extends T> coll)返回集合中的最小元素同上-
max(Collection<? extends T> coll, Comparator<? super T> comp)使用比较器返回最大元素需提供 Comparator-
min(Collection<? extends T> coll, Comparator<? super T> comp)使用比较器返回最小元素同上-
反转reverse(List<?> list)反转列表元素顺序--
随机排序shuffle(List<?> list)随机打乱列表顺序-默认使用 Random 类
shuffle(List<?> list, Random rnd)使用指定随机源打乱顺序需提供 Random 对象-
填充fill(List<? super T> list, T obj)用指定对象填充列表所有元素目标列表长度不变-
复制copy(List<? super T> dest, List<? extends T> src)将源列表元素复制到目标列表目标列表长度 ≥ 源列表长度直接覆盖目标列表元素
交换swap(List<?> list, int i, int j)交换列表中两个索引位置的元素i 和 j 必须在列表有效范围内-

三、综合应用

1、操作学生信息如下:

  1. 创建学生列表
  2. 添加学生
  3. 打印原始学生列表
  4. 对学生列表进行排序(按年龄升序)
  5. 查找年龄最大的学生
  6. 查找年龄最小的学生
  7. 反转学生列表
  8. 替换年龄最小的学生的信息
  9. 填充学生列表
  10. 复制学生列表
  11. 交换两个学生的位置
  12. 旋转学生列表
  13. 随机打乱学生列表
  14. 使用二分查找查找特定学生(按年龄排序后,但示例中查找的年龄与注释不匹配)

2、编码

   Student类

public class Student {private String id;private String name;private int age;public Student(String id, String name, int age) {this.id = id;this.name = name;this.age = age;}// Getters 和 Setterspublic String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{id='" + id + "', name='" + name + "', age=" + age + "}";}}
StudentManager类
public class StudentManager {public static void main(String[] args) {// 创建学生列表List<Student> students = new ArrayList<>();// 添加学生students.add(new Student("001", "Alice", 22));students.add(new Student("002", "Bob", 25));students.add(new Student("003", "Charlie", 23));students.add(new Student("004", "Diana", 21));// 打印原始学生列表System.out.println("原始数据:");for (Student student : students) {System.out.println(student);}// 对学生列表进行排序(按年龄升序)Collections.sort(students, Comparator.comparing(Student::getAge));System.out.println("\n按年龄升序:");for (Student student : students) {System.out.println(student);}// 查找年龄最大的学生Student oldestStudent = Collections.max(students, Comparator.comparing(Student::getAge));System.out.println("\n年龄最大的学生:");System.out.println(oldestStudent);// 查找年龄最小的学生Student youngestStudent = Collections.min(students, Comparator.comparing(Student::getAge));System.out.println("\n年龄最小的学生:");System.out.println(youngestStudent);// 反转学生列表Collections.reverse(students);System.out.println("\n反转:");for (Student student : students) {System.out.println(student);}// 替换年龄最小的学生的信息(例如,替换为另一个学生)Collections.replaceAll(students, youngestStudent, new Student("005", "Eve", 20));System.out.println("\n替换:");for (Student student : students) {System.out.println(student);}// 填充学生列表(用特定学生填充)Collections.fill(students, new Student("006", "Fiona", 24));System.out.println("\n填充:");for (Student student : students) {System.out.println(student);}// 复制学生列表List<Student> copiedStudents = new ArrayList<>(students);Collections.copy(copiedStudents, students);System.out.println("\n复制:");for (Student student : copiedStudents) {System.out.println(student);}// 交换两个学生的位置,例如,将第一个和最后一个学生交换位置Collections.swap(students, 0, students.size() - 1);System.out.println("\n交换:");for (Student student : students) {System.out.println(student);}// 旋转学生列表,例如,将列表向右旋转2个位置Collections.rotate(students, 2);System.out.println("\n旋转:");for (Student student : students) {System.out.println(student);}// 随机打乱学生列表Collections.shuffle(students);System.out.println("\n随机打乱:");for (Student student : students) {System.out.println(student);}// 使用二分查找查找特定学生(按年龄排序后)
//        Student targetStudent = new Student("000", "Non-existent", 21); // 假设要查找年龄为21的学生Student targetStudent = new Student("000", "Non-existent", 27); // 假设要查找年龄为27的学生int index = Collections.binarySearch(students, targetStudent, Comparator.comparing(Student::getAge));if (index >= 0) {System.out.println("\n年龄是21的学生索引: " + index);System.out.println(students.get(index));} else {System.out.println("\n没有找到年龄是21的学生索引: " + (-index - 1));}}
}

3、效果

 

四、总结

      Collections 类提供的方法大大简化了对集合的操作,通过合理的选择和使用这些方法,可以高效地实现各种集合处理逻辑。需要注意的是,某些方法(如排序和二分查找)对集合的状态(如排序状态)有要求,使用时需要确保满足这些要求,以避免异常或错误的结果。

 

http://www.dtcms.com/wzjs/449923.html

相关文章:

  • 天水 网站建设招聘厦门人才网最新招聘信息
  • 关于网站建设的请示地推
  • 企业网站建设物美价廉广州seo优化电话
  • 网站域名后缀代表什么steam交易链接在哪复制
  • 网站建设是什么行业什么是优化师
  • 中山建设网站互联网公司排名
  • 只做网站百度搜索指数是怎么计算的
  • 网站后台管理系统模板 html学seo网络推广
  • 西安自助建站做网站智能网站推广优化
  • 一品威客网是做啥的网站百度指数官网入口
  • 网站建设公司的那些坑优化大师是什么
  • 做区位分析底图的网站模板建站的网站
  • 天津企业网站定制服务在线网站分析工具
  • 网站打不开的原因百度问问我要提问
  • 网站营销网站优化网站 软件
  • 广州做网站的核心关键词是什么意思
  • 淘宝上做网站排名的是真的吗成都网站改版优化
  • 网站收录查询方法百度pc网页版登录入口
  • cuteftp可以上传网站吗今天最新新闻
  • 网站建设推广服务网站推广策划报告
  • 政府网站比较网站子域名查询
  • 郑州公司建站搭建4414站长平台
  • 广东两学一做考试网站山东疫情最新情况
  • phpweb绿色大气茶叶网站源码优化关键词的步骤
  • 网站建设的具体过程seo服务是什么意思
  • 手机网站 栏目定制福州seo公司
  • 网站建设考试卷a卷交换友情链接的好处
  • 网站做搜索引擎优化小程序推广平台
  • 法人一证通主副证书管理新流程思亿欧seo靠谱吗
  • 政府网站群建设项目网络推广岗位职责和任职要求