类与方法的使用
文章目录
一、String和StringBuilder
- 遍历字符串:数组名+charAt(i);(i用for循环遍历)
- 字符串拼接
- 字符串反转
直接在string中将字符串拼接和反转较为麻烦啦,可将其转为StringBuilder,在进行相关操作
StringBuilder中拼接方法为:对象.append
StringBuilder中反转方法为:对象.reverse
public static void main(String[] args) {
String a = "hello";
StringBuilder a1 = new StringBuilder(a); //将string转换为StringBuilder
StringBuilder a2 = new StringBuilder(a);
a1.append("world"); //拼接
System.out.println(a1);
a2.append("world").reverse(); //反转
System.out.println(a2);
String b1 = a1.toString(); //将StringBuilder拼接后的变量转换为string并输出
System.out.println(b1);
String b2 = a2.toString(); //将StringBuilder反转后的变量转换为string并输出
System.out.println(b2);
}
二、Math类
用静态static修饰,可直接(类.方法名)使用。如:Math.abs();
介绍Math中几个常用方法的使用:
方法名 | 作用 |
---|---|
int abs(int a) | 返回参数的绝对值 |
double ceil (double a) | 返回大于或等于参数的最小double值,等于一个整数 |
double floor (double a) | 返回小于或等于参数的最大double值,等于一个整数 |
int round (float a) | 按照四舍五入返回最接近参数的int |
int max (int a, int b) | 返回两个int值中的最大值 |
int min (int a, int b) | 返回两个int值中的最小值 |
double pow (double a, double b) | 返回a的b次幂的值 |
double random () | 返回值为double的正直。[0.0,1.0) |
代码实现:
public static void main(String[] args) {
// public static int abs(int a); 返回参数的绝对值
System.out.println(java.lang.Math.abs(88));
System.out.println(java.lang.Math.abs(-88));
System.out.println("-------------------");
// public static double ceil (double a); 返回大于或等于参数的最小double值,等于一个整数
System.out.println(java.lang.Math.ceil(12.34));
System.out.println(java.lang.Math.ceil(12.56));
System.out.println("-------------------");
// public static double floor (double a); 返回小于或等于参数的最大double值,等于一个整数
System.out.println(java.lang.Math.floor(12.34));
System.out.println(java.lang.Math.floor(12.56));
System.out.println("-------------------");
// public static int round (float a); 按照四舍五入返回最接近参数的int
System.out.println(java.lang.Math.round(12.34F));
System.out.println(java.lang.Math.round(12.56F));
System.out.println("-------------------");
// public static int max (int a, int b); 返回两个int值中的最大值
System.out.println(java.lang.Math.max(15,34));
System.out.println("-------------------");
// public static int min (int a, int b); 返回两个int值中的最小值
System.out.println(java.lang.Math.min(15,34));
System.out.println("-------------------");
// public static double pow (double a, double b); 返回a的b次幂的值
System.out.println(java.lang.Math.pow(2.0,6.0));
System.out.println("-------------------");
// public static double random (); 返回值为double的正直。[0.0,1.0)
System.out.println(java.lang.Math.random());
//若要取100内随机数,则
System.out.println((int) (java.lang.Math.random()*100));
}
}
三、System类
用静态static修饰,可直接(类.方法名)使用。如:system.exit(0);
介绍Math中几个常用方法的使用:
方法名 | 作用 |
---|---|
public static void exit(int status) | 终止当前运行的 JAVA 虚拟机,非零表示异常终止 |
public static long currentTimeMillis() | 返回当前距离1971年1月1日的时间(以毫秒为单位) |
public static void main(String[] args) {
// public static void exit(int status); 终止当前运行的 JAVA 虚拟机,非零表示异常终止
/*
System.out.println("开始");
System.exit(0); //仅输出开始便结束程序,不输出结束
System.out.println("结束");
*/
// public static long currentTimeMillis(); 返回当前距离1971年1月1日的时间(以毫秒为单位)
System.out.println(System.currentTimeMillis());
//将时间转化为年
System.out.println(System.currentTimeMillis()*1.0/1000/60/60/24/365+"年");
//查看遍历并输出i需要花多长时间
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("共耗时:"+(end - start) +"毫秒");
}
四、Object类
介绍Object类中常用的两种方法
方法名 | 作用 |
---|---|
public String toString() | 返回对象的字符串表示形式。建议所有子类重写该方法,自动生成 |
public boolean equals(Object obj) | 比较对象是否相等。默认比较地址,重写可以比较内容,自动生成 |
(该部分内容比较详细,通过查看源码了解其作用原理,故内容较多)
toString方法:
不重写返回的是:com.xu.ObjDeno.Student@1b6d3586,重写返回的是:Student{name=‘大哥大’, age=23}
/*
创建一个学生类,定义名字和年龄的私有属性、创建有参和无参构造方法及get/set方法,最后加入一个重写toString方法
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
*
public String toString() 返回对象的字符串表示形式。建议所有子类重写该方法,自动生成
*/
import com.xu.ObjDeno.MytoString.Student;
public class Test {
public static void main(String[] args) {
Student s = new Student();
s.setName("大哥大");
s.setAge(23);
System.out.println(s);
// 想要得到(名字,年龄)的结果,可结果是com.xu.ObjDeno.Student@1b6d3586
/*
查看println方法的底层实现(源码):(快捷键:Ctrl+B)
public void println(Object x) { //x = s
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
public static String valueOf(Object obj) { //obj = x
return (obj == null) ? "null" : obj.toString();
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
*/
//逐步分析下来发现,其实现的过程与下面这个一样:
System.out.println(s.toString()); //com.xu.ObjDeno.Student@1b6d3586
//官方建议:所有子类重写此方法(tostring)
//在Student类中,自动重写快捷键:Alt+Ins
/*
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
*/
//输出结果为: Student{name='大哥大', age=23}
// Student{name='大哥大', age=23}
}
}
equals方法:
不重写比较的是地址,重写比较的是内容!多用与字符串内容的比较!
/*
创建一个学生类,定义名字和年龄的私有属性、创建有参和无参构造方法及get/set方法,最后加入一个重写equals方法、并深入解析
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public boolean equals(Object o) {
/*
s1.equals(s2)
this --- s1
o --- s2
*/
//比较地址是否相同,如果相同,直接返回true
if (this == o) return true;
//先判断参数是否为null,接着判断两个对象是否来自同一个类
if (o == null || getClass() != o.getClass()) return false;
//向下转型
Student student = (Student) o; // Student -- s2
//判断年龄是否相等(if语句中,前一个age隐藏一个this)
if (age != student.age) return false;
//比较姓名内容是否相同
return name != null ? name.equals(student.name) : student.name == null;
}
}
/*
public boolean equals(Object obj) 比较对象是否相等。默认比较地址,重写可以比较内容,自动生成
*/
public class Test {
public static void main(String[] args) {
//给对象赋值
Student s1 = new Student();
s1.setName("大哥大");
s1.setAge(23);
Student s2 = new Student();
s2.setName("大哥大");
s2.setAge(23);
//需求:比较两个对象的内容是否相同
// System.out.println(s1 == s2); //s1、s2比较的是地址值,不是内容
System.out.println(s1.equals(s2)); // false
/*
public boolean equals(Object obj) {
//this --- s1
//obj --- s2 s1、s2还是地址值,因此返回的还是false,
return (this == obj); //因此需要重写方法(在Student中重写)
}
*/
//在Student中重写方法后,输出True
}
}
五、冒泡排序(Arrays)
Arrays学习的铺垫
/*
冒泡排序:对要进行排序的数据中相邻的数据进行两两比较,然后将较大的放在后面,直至所有数据按要求完成排序
*/
public class Test {
public static void main(String[] args) {
//定义一个数组
int[] arr ={24,69,80,57,13};
System.out.println("排序前:"+ Arrays.toString(arr)); // 排序前:[24, 69, 80, 57, 13]
/*
//第一次排序
for (int i = 0; i < arr.length-1-0; i++) {
if (arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("第一次排序后:"+Arrays.toString(arr)); //第一次排序后:[24, 69, 57, 13, 80]
//第二次排序
for (int i = 0; i < arr.length-1-1; i++) {
if (arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("第二次排序后:"+Arrays.toString(arr)); //第二次排序后:[24, 57, 13, 69, 80]
//第三次排序
for (int i = 0; i < arr.length-1-2; i++) {
if (arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("第三次排序后:"+Arrays.toString(arr)); //第三次排序后:[24, 13, 57, 69, 80]
//第四次排序
for (int i = 0; i < arr.length-1-3; i++) {
if (arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("第四次排序后:"+Arrays.toString(arr)); //第四次排序后:[13, 24, 57, 69, 80]
*/
//将以上代码进行优化:
for (int x = 0; x < arr.length-1; x++) {
for (int i = 0; i < arr.length - 1 -x; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
System.out.println("最后排序:" + Arrays.toString(arr)); //最后排序:[13, 24, 57, 69, 80]
}
}
Arrays类
介绍Arrays类中常用的两种方法:
方法名 | 作用 |
---|---|
public static String toString(int[] a) | 返回指定数组的内容的字符串表达形式。(遍历数组) |
public static void sort(int[] a) | 按照数字顺序排列指定的数组。(将数据从低到高排序) |
*
Arrays 属于工具类的设计思想:
构造方法用private修饰
成员用public static修饰
*/
import java.util.Arrays;
public class Test2 {
// public static String toString(int[] a); 返回指定数组的内容的字符串表达形式
// public static void sort(int[] a); 按照数字顺序排列指定的数组
public static void main(String[] args) {
int[] a = {25,64,81,44,32,65,13,85};
System.out.println("排序前:"+ Arrays.toString(a));
Arrays.sort(a);
System.out.println("排序后:"+ Arrays.toString(a));
/*
输出结果:
排序前:[25, 64, 81, 44, 32, 65, 13, 85]
排序后:[13, 25, 32, 44, 64, 65, 81, 85]
*/
}
}
六、基本类型包装类
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。
常用操作之一:用于基本数据类型与字符串间的转换。
基本数据类型 | 包装类 |
---|---|
bety | Bety |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer 类
包装一个对象中的原始类型int值。
主要介绍下列常见的四个方法:
方法名 | 说明 |
---|---|
public Integer(int value) | 根据 int 值创建 Integer 对象(过时) |
public Integer(String s) | 根据 String 值创建 Integer 对象(过时) |
public static Integer valueOf (int i) | 返回表示指定的 int 值的 interger 实例 |
public static Integer valueOf (String s) | 返回一个保存指定值的 interger 对象 String |
public static int parseInt(String s) | 将字符串解析为int类型,该方法是Integer类中的方法 |
/*
构造方法:
public Integer(int value); 根据 int 值创建 Integer 对象(过时)
public Integer(String s); 根据 String 值创建 Integer 对象(过时)
静态方法获取对象
public static Integer valueOf (int i); 返回表示指定的 int 值的 interger 实例
public static Integer valueOf (String s); 返回一个保存指定值的 interger 对象 String
*/
public static void main(String[] args) {
//public Integer(int value); 根据 int 值创建 Integer 对象(过时)
Integer i1 = new Integer(100);
System.out.println(i1); //提示过时
//public Integer(String s); 根据 String 值创建 Integer 对象(过时)
Integer i2 = new Integer("200");
// Integer i2 = new Integer("abc"); //报错 NumberFormatException 必须是由数字组成的字符串
System.out.println(i2); //提示过时
//public static Integer valueOf (int i); 返回表示指定的 int 值的 interger 实例
Integer i3 = Integer.valueOf(300);
System.out.println(i3);
//public static Integer valueOf (String s); 返回一个保存指定值的 interger 对象 String
Integer i4 = Integer.valueOf("400");
// Integer i4 = Integer.valueOf("abc"); //报错 NumberFormatException 必须是由数字组成的字符串
System.out.println(i4);
}
}
int 和 string之间的转换
public static void main(String[] args) {
// int ---> String
int a = 100;
//方式一:
String a1 = "" + a;
System.out.println(a1);
//方式二
//public static String valueOf(int i)
String a2 = String.valueOf(a);
System.out.println(a2);
// String ---> int
String b = "200";
//方式一:string --> Integer --> int
Integer b1 = Integer.valueOf(b);
int b11 = b1.intValue();
System.out.println(b11);
//方式二
//public static int parseInt(String s)
int b2 = Integer.parseInt(b);
System.out.println(b2);
}
}
/*
总结:
1、int 转换为 String
public static String valueOf(int i); 返回int参数的字符串表示形式,该方法是String类中的方法
2、String 转换为 int
public static int parseInt(String s); 将字符串解析为int类型,该方法是Integer类中的方法
*/
七、实例
需求:有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:“27 38 46 50 91”(字符串)
/*
思路:
1、定义一个字符串
2、把字符串中的数字数据存储到一个int类型的数组中
如何得到字符串中每一个数字数据? public String split(String regex)
定义一个int数组,把String[]数组中的每一个元素存储到int数组中. public static int parseInt(String s)
3、对 int 数组进行排序
4、把排序后的int 数组中的元素进行拼接得到一个字符串,
5、输出结果
*/
public class Exmp {
public static void main(String[] args) {
//定义一个字符串
String number = "91 27 46 38 50";
System.out.println("排序前:"+ number); // 排序前:91 27 46 38 50
//把字符串中的数字数据存储到一个int类型的数组中
String[] sta = number.split(" "); //将字符串用split方法放到String类型的数组中
int[] arr =new int[sta.length]; //定义一个数组
for (int i = 0; i < sta.length; i++) {
arr[i] = Integer.parseInt(sta[i]); //将String转换为int
}
//对 int 数组进行排序
Arrays.sort(arr);
//把排序后的int 数组中的元素进行拼接得到一个字符串.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
//在元素之间添加空格
if(i != arr.length-1){
sb.append(arr[i]+" "); //连接各个数据
}else {
sb.append(arr[i]);
}
}
//输出结果
String sb1 = sb.toString(); //将StringBuilder类型转换为String
System.out.println("排序后:"+sb1); //排序后:27 38 46 50 91
}
}
八、自动装箱和拆箱
装箱:把基本数据类型转换为对应的包装类类型。
拆箱:把包装类类型转换为对应的基本数据类型。
public static void main(String[] args) {
//装箱:把基本数据类型转换为对应的包装类类型。
Integer i = Integer.valueOf(100); //装箱
Integer ii = 100; //自动装箱 其底层内部也做了Integer.valueOf(100)
//拆箱:把包装类类型转换为对应的基本数据类型。
ii = ii.intValue()+200; //拆箱
ii += 200; //自动拆箱 其底层内部隐含了自动装箱和拆箱
//思考:
Integer iii = null;
if (iii != null) {
iii += 300;
System.out.println(iii); //NullPointerException
} // iii是null,null调了Integer.valueOf(iii)方法,因此是空指针
//因此,一般都要加个判断
}