Java 包装类和Arrays类的介绍
csv
文章目录
Solidity
包装类
包装类其实就是8种基本数据类型对应的引用类型。
NRF24L01
基本数据类型 | 引用数据类型 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
char | Character |
float | Float |
double | Double |
boolean | Boolean |
为什么需要包装类?
JVM内存模型
Java为了实现一切皆对象,为8种基本类型提供了对应的引用类型。
自助报表
后面的集合和泛型其实也只能支持包装类型,不支持基本数据类型。
烟花
自动装箱:
项目管理工具
基本类型的数据和变量可以直接赋值给包装类型的变量。
瑞吉外卖
public static void main(String[] args) {
int num1 = 10;
// 自动装箱(将int基本数据类变量赋值给包装类型变量)
Integer res1 = num1;
System.out.println(res1);
}
自动拆箱:
npm配置
包装类型的变量可以直接赋值给基本数据类型的变量。
资源
public static void main(String[] args) {
Integer num2 = 100;
// 自动拆箱(将包装类型变量赋值给基本类型变量)
int res2 = num2;
System.out.println(res2);
}
包装类的特有功能:
chrome devtools
包装类的变量的默认值可以是null,容错率更高, 而基本类型不可以。
INA226
public static void main(String[] args) {
Integer num3 = null;
}
可以把基本类型的数据转换成字符串类型(用处不大)
Oracle数据库SQL优化
- 调用toString()方法得到字符串结果。
- 或者调用Integer.toString(基本类型的数据)。
public static void main(String[] args) {
Integer number = 100;
// 方式一
String result1 = number.toString();
// 方式二
String result2 = Integer.toString(101);
// 方式三: 最简单常用
String result3 = number + "";
System.out.println(result1 + 1); // 1001
System.out.println(result2 + 1); // 1011
System.out.println(result3 + 2); // 1002
}
可以把字符串类型的数值转换成真实的数据类型(非常有用)
分组
- teger.parseInt(“字符串类型的整数”)
- Double.parseDouble(“字符串类型的小数”)。
public static void main(String[] args) {
String strNum1 = "123";
String strNum2 = "12.123";
// 字符串转整数
int intNum = Integer.parseInt(strNum1);
// 字符串转小数
double doubleNum = Double.parseDouble(strNum2);
System.out.println(intNum + 1); // 124
System.out.println(doubleNum + 1); // 13.123
}
将字符串类型的数字转为真实的数据, 我们也可以调用包装类的valueOf方法
ubisoft
public static void main(String[] args) {
String strNum1 = "123";
String strNum2 = "12.123";
// 字符串转整数
int intNum = Integer.valueOf(strNum1);
// 字符串转小数
double doubleNum = Double.valueOf(strNum2);
System.out.println(intNum + 1); // 124
System.out.println(doubleNum + 1); // 13.123
}
小结:
IDE瀹夎
包装类是什么,作用是什么?
音视频
基本数据类型对应的引用类型,实现了一切皆对象。
addLast
后期集合和泛型不支持基本类型,只能使用包装类。
ViT
包装类有哪些特殊功能?
热更新
可以把基本类型的数据转换成字符串类型(用处不大)
mmaction2
可以把字符串类型的数值转换成真实的数据类型(非常有用)
博客
Arrays类
Arrays类的概述
java-rocketmq
数组操作工具类,专门用于操作数组元素的。
uni.getLocation
Arrays类的常用API
Spring 定时任务
方法名 | 说明 |
---|---|
toString(类型[] a) | 返回数组的内容(字符串形式) |
sort(类型[] a) | 对优质的数组默认进行升序排序 |
sort(类型[] a, Comparator<? super T> c) | 使用比较器对象自定义排序 |
binarySearch(int[] a, int key) | 二分搜索数组中的数据,存在返回索引,不存在返回-1 |
toString方法
人体姿态估计
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 0, 100};
// 返回数组内容
String res = Arrays.toString(arr);
System.out.println(res); // [10, 20, 30, 40, 0, 100]
}
sort方法
ldr
public static void main(String[] args) {
int[] arr = {20, 10, 900, 40, 0, 100};
// 升序排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // [0, 10, 20, 40, 100, 900]
}
binarySearch方法, 二分查找法搜索数组中的数据, 找到返回索引, 找不到返回-1
赵彦军
注意: 前提被查找的数组必须是排好序的
Hudi
public static void main(String[] args) {
int[] arr = {20, 10, 900, 40, 0, 100};
// 升序排序
Arrays.sort(arr);
// 查找数组
int index = Arrays.binarySearch(arr, 10);
System.out.println(index); // 1
}
sort方法自定义规则:
JavaSE
sort方法第二个参数是Comparator, 设置Comparator接口对应的比较器对象,可以来自己定制比较规则。
注意: 自定义比较器对象只支持引用型类型的排序
如果认为左边数据大于右边数据, 返回正整数
如果认为左边数据小于右边数据, 返回负整数
如果认为左边数据等于右边数据, 返回0
默认的升序是o1 – o2; 降序是o2 – o1
public static void main(String[] args) {
// 只支持引用类型, 因此定义包装类数据类型
Integer[] arr = {10, 0, 30, 50, 100, 67};
// 调用sort方法, 降序
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
System.out.println(Arrays.toString(arr)); // [100, 67, 50, 30, 10, 0]
}