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

Java的Arrays类

1.返回数组的内容

int[]arr1={1,2,3,4};String s = Arrays.toString(arr1);System.out.println(s);

2.拷贝数组(指定范围,包前不包后)

int[]arr1={1,2,3,4};String s = Arrays.toString(arr1);System.out.println(s);int[] copy = Arrays.copyOfRange(arr1, 1, 4);System.out.println(copy);//直接输出数组//返回的是地址System.out.println(Arrays.toString(copy));

3.指定新数组的长度----用于扩容数组

int[]arr1={1,2,3,4};String s = Arrays.toString(arr1);System.out.println(s);int[] copy = Arrays.copyOfRange(arr1, 1, 4);System.out.println(copy);//直接输出数组//返回的是地址System.out.println(Arrays.toString(copy));int[] copy1 = Arrays.copyOf(arr1, 10);System.out.println(Arrays.toString(copy1));

4.把数组中的原数据改为新数据存进去

double[]price={11,22,13};Arrays.setAll(price, new IntToDoubleFunction() {@Overridepublic double applyAsDouble(int value) {return price[value]*0.8;}});System.out.println(Arrays.toString(price));

5.对数组进行排序(默认是升序排序)

int[]arr1={2,1,3,4};Arrays.sort(arr1);System.out.println(Arrays.toString(arr1));
public class Student implements Comparator<Student> {String name;int year;String tel;double height;​public Student() {}​public Student(String name, double height, String tel, int year) {this.name = name;this.height = height;this.tel = tel;this.year = year;}​public String getName() {return name;}​public void setName(String name) {this.name = name;}​public double getHeight() {return height;}​public void setHeight(double height) {this.height = height;}​public String getTel() {return tel;}​public void setTel(String tel) {this.tel = tel;}​public int getYear() {return year;}​public void setYear(int year) {this.year = year;}Student s1 = new Student("张三", 178.2, "1223434", 12);Student s2 = new Student("李四", 168.0, "1345656", 42);Student s3 = new Student("小明", 170.5, "1451345", 30);Student s4 = new Student("小王", 175.1, "1456112", 28);Student[] students = new Student[4];students[0]=s1;students[1]=s2;students[2]=s3;students[3]=s4;//第一种方法Arrays.sort(students, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return Double.compare(o1.getHeight(), o2.getHeight());}});System.out.println(Arrays.toString(students));public class Student implements Comparator<Student> {String name;int year;String tel;double height;​public Student() {}​public Student(String name, double height, String tel, int year) {this.name = name;this.height = height;this.tel = tel;this.year = year;}​public String getName() {return name;}​public void setName(String name) {this.name = name;}​public double getHeight() {return height;}​public void setHeight(double height) {this.height = height;}​public String getTel() {return tel;}​public void setTel(String tel) {this.tel = tel;}​public int getYear() {return year;}​public void setYear(int year) {this.year = year;}​@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", year=" + year +", tel='" + tel + '\'' +", height=" + height +'}';}​@Overridepublic int compare(Student o1, Student o2) {return o1.year-o2.year;}}Student s1 = new Student("张三", 178.2, "1223434", 12);Student s2 = new Student("李四", 168.0, "1345656", 42);Student s3 = new Student("小明", 170.5, "1451345", 30);Student s4 = new Student("小王", 175.1, "1456112", 28);Student[] students = new Student[4];students[0]=s1;students[1]=s2;students[2]=s3;students[3]=s4;//第二种方法System.out.println(Arrays.toString(students));}

 


文章转载自:

http://lxVP9cK3.dnjwm.cn
http://UXGFaYW7.dnjwm.cn
http://0MFeclXr.dnjwm.cn
http://um1c4MeM.dnjwm.cn
http://wQxEUBts.dnjwm.cn
http://1ZIvytco.dnjwm.cn
http://VYP7mbAt.dnjwm.cn
http://o9uRJugD.dnjwm.cn
http://YrORFxxa.dnjwm.cn
http://V9ZNAsYk.dnjwm.cn
http://mNHPqQ91.dnjwm.cn
http://ciG7PSOq.dnjwm.cn
http://npDDzbHF.dnjwm.cn
http://ZRBgdy7U.dnjwm.cn
http://7DHMwsBA.dnjwm.cn
http://5XdVSyWy.dnjwm.cn
http://UItvggPr.dnjwm.cn
http://iQHdl1kI.dnjwm.cn
http://vDWIxFIY.dnjwm.cn
http://qo0Q6vSd.dnjwm.cn
http://gsqw45nd.dnjwm.cn
http://TDX9wOaA.dnjwm.cn
http://rcvAMMhv.dnjwm.cn
http://LQ58yF0R.dnjwm.cn
http://WYHThYlH.dnjwm.cn
http://MUCv8YHm.dnjwm.cn
http://551BVXrw.dnjwm.cn
http://EsyAXqO8.dnjwm.cn
http://MXeJVj7z.dnjwm.cn
http://RmbbrYNP.dnjwm.cn
http://www.dtcms.com/a/385579.html

相关文章:

  • 每天认识一个电子器件之LED灯
  • 每日前端宝藏库 | anime.js⏳✨
  • CSS脉冲光环动画效果
  • C++ 之【C++11的简介】(可变参数模板、lambda表达式、function\bind包装器)
  • 【基础组件 and 网络编程】对 DPDK 的 MPMC 无锁队列 rte-ring 组件的思考分析(同时也是实战原子操作的好机会)
  • ingress-nginx-controller 414 Request—URI Too Large
  • Java 定时任务与分布式调度工具分析
  • 【热点】最优传输(Optimal Transport)及matlab案例
  • 用 Kotlin 玩转 Protocol Buffers(proto3)
  • leecode73 矩阵置零
  • SELECT INTO 和 INSERT INTO SELECT 区别
  • dhtmlx-gantt
  • Spring如何巧妙解决循环依赖问题
  • 第四章:职业初印象:打造你的个人品牌(1)
  • (九)Python高级应用-文件与IO操作
  • FFmpeg06:SDL渲染
  • javadoc命令 错误: 编码 GBK 的不可映射字符 (0x80)
  • 【面试场景题】自增主键、UUID、雪花算法都有什么问题
  • 数据整理器(Data Collators)总结 (95)
  • 代码评价:std::shared_ptr用法分析
  • 23种设计模式案例
  • AI Agent案例与实践全解析:字节智能运维
  • MyBatis-Plus分页插件实现导致total为0问题
  • S32DS仿真环境问题
  • 黑马JavaWeb+AI笔记 Day07 Web后端实战(部门管理模块)
  • 【AI开发】【前后端全栈】[特殊字符] AI 时代的快速开发思维
  • kimi-k2论文阅读笔记
  • [SC]一个使用前向声明的SystemC项目例子
  • Gunicorn 部署与调优全指南(2025 版)
  • 第二十一篇|新宿平和日本语学校的结构化解读:费用函数、文化网络与AI教育建模