javaAPI几种常用的方法

1.object
重写toString,equals,hashcode方法
Demo1

package object1;
//object的otString方法
class Person{
	int id;
	String name;
	public Person(int id, String name) {
		this.id = id;
		this.name = name;
	}
	public Person() {
		
	}
	//重写toString方法,以输出你想要的输出格式
	public String toString() {
		return "编号:"+ this.id+" ,姓名:"+this.name;
	}
	//重写equals方法
	public boolean equals(Object obj) {
		Person p = (Person) obj;
		return this.id == p.id;
	}
	//重写hashcode方法,Object返回的是对象的hash码,理解为内存地址。但是在这里我们需要
	public int hashCode( ) {
		return this.id;
	}
}

public class Demo1 {
	public static void main(String[] args) {
		Person p1 = new Person(1,"lesen");
		Person p2 = new Person(1,"lesen");
		System.out.println(p1);
		System.out.println("是否为同一个对象:"+p1.equals(p2));
		System.out.println("是否为同一个对象:"+p1.hashCode());
		System.out.println("是否为同一个对象:"+p2.hashCode());
	}
}

Demo2

package object1;
//Object的equals方法

class Student {
	String name;
	int grade;
	public  Student(String name,int grade) {
	this.name = name;
	this.grade = grade;
	}
	public Student() {	
	}
	//重写toString方法
	public String  toString() {
		return "学生"+name+"的考试成绩为:"+grade;
	}
	//重写equals
	public boolean equals(Object obj) {
		Student s = (Student) obj;
		return this.grade == s.grade;
	}
	//重写hashCode
	public int hashCode() {
		return this.grade;
	}
}

public class Demo2 {
	public static void main(String[] args) {
		Student s1 = new Student("lisen",98);
		Student s2 = new Student("lisen",98);
		System.out.println(s1);
		System.out.println(s1.equals(s2));
		System.out.println(s1.hashCode());
		System.out.println(s2.hashCode());
	}
}

2.String类
2.1去除字符串前后的空格符号

package string1;
//去除字符串前后的空格符号
public class Demo1 {
	public static void main(String[] args) {
		String str ="    中国 是世界第二 大 经济体   ";
		System.out.println(myTrim(str));
	}
	public static String myTrim(String str) {
		char[] ch = str.toCharArray();
		int startIndex =0;
		int endIndex = ch.length-1;
		while(true) {
			if(ch[startIndex] == ' ') {
				startIndex++;
			}else {
				break;
			}
			if(ch[endIndex] == ' ') {
				endIndex--;
			}else {
				break;
			}
		}
		return str.substring(startIndex,endIndex+1);
	}
}

2.2

package string1;
//自己实现Trim方法
import java.util.Arrays;

public class Demo2 {
	public static void main(String[] args) {
		String str = new String();
		str ="happy fresh day!";
		System.out.println(str);
		byte[] by = {97,98,99,100,101};
		str = Arrays.toString(by);
		System.out.println(str);
		str = new String(by);
		System.out.println(str);
		char[] ch = {'a','b','c'};
		str = new String(ch);
		System.out.println(str);
		int[] arr = {65,66,67};
		str = new String(arr,0,2);
		System.out.println(str);
		str ="pray for lucky things";
		int a =str.indexOf("l");
		str = new String(str);
		System.out.println(str);
		System.out.println("字符串的长度:"+str.length());
		System.out.println("获取特定位置的字符:"+str.charAt(3));
		System.out.println("获取字符出现的位置:"+a);
		System.out.println("获取字符最后一次出现的位置:"+str.lastIndexOf('y'));
		System.out.println("获取特定位置的字符:"+Arrays.toString(str.toCharArray()));
	}

}

2.3

package string1;
//获取上传文件名“D:\\20190412\\Demo.java”
public class Demo3 {
	public static void main(String[] args) {
		String str = new String();
		str = "D:\\20190412\\Demo.java";
	   getFileName(str);
		}
	public static void getFileName(String str) {
		int targetIndex = str.lastIndexOf("\\");
		String fileName = str.substring(targetIndex+1);
		System.out.println(fileName);
	}
}

2.4

package string1;
//字符串翻转
public class Demo4 {
	public static void main(String[] args) {
		String str = "新中国好";
		System.out.println(reversal(str));
	}
	
	public static String reversal(String str) {
		char[] ch = str.toCharArray();  //将字符串转换为字符数组
		for(int i =0; i<ch.length;i++) {
			//翻转字符串内容
			if(i<ch.length-i) {
				char temp = ch[i];
				ch[i] = ch[ch.length-1-i];
				ch[ch.length-1-i] = temp;
			}else {
				break;
			}
		}
		return new String(ch);
	}
}

2.5

package string1;
//求子串在整串中出现的次数
public class Demo5 {
		public static void main(String[] args) {
		String str ="美丽的中国的中部城市有很多让中意的意中人";
		getTimes(str,"中");
		}
		
		public static void getTimes(String str,String target) {
			int count=0;
			int startIndex =0;
			while((startIndex  =str.indexOf("中",startIndex))  != -1) {
				count ++;
				startIndex = startIndex + target.length();
			}
			System.out.println(count);
		} 
}

3.StringBuffer
4.StringBuilder
5.System

package string1;
//求子串在整串中出现的次数
public class Demo5 {
		public static void main(String[] args) {
		String str ="美丽的中国的中部城市有很多让中意的意中人";
		getTimes(str,"中");
		}
		
		public static void getTimes(String str,String target) {
			int count=0;
			int startIndex =0;
			while((startIndex  =str.indexOf("中",startIndex))  != -1) {
				count ++;
				startIndex = startIndex + target.length();
			}
			System.out.println(count);
		} 
}

6.Date

package date1;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Demo {
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance(); //获取当前的系统时间。
		System.out.println("年:"+ calendar.get(Calendar.YEAR));
		System.out.println("月:"+ (calendar.get(Calendar.MONTH)+1));
		System.out.println("日:"+ calendar.get(Calendar.DATE));
		
		System.out.println("时:"+ calendar.get(Calendar.HOUR_OF_DAY));
		System.out.println("分:"+ calendar.get(Calendar.MINUTE));
		System.out.println("秒:"+ calendar.get(Calendar.SECOND));
		Date date = new Date(); //获取当前的系统时间。
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss") ; //使用了默认的格式创建了一个日期格式化对象。
		String time = dateFormat.format(date);  //可以把日期转换转指定格式的字符串
		System.out.println("当前的系统时间:"+ time); 
	}

}

7.Math

package Math;
//制作验证码
import java.util.Random;

public class Demo2 {
	public static void main(String[] args) {
		char[] arr = {'中','国','好','a'};
		Random random = new Random();
		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i<4; i++) {
			int index = random.nextInt(arr.length);
			sb.append(arr[index]);
		}
		System.out.println("验证码:"+sb);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值