Math类
package chapter16.Math类;
public class MathMethod {
public static void main(String[] args) {
//全是静态方法
System.out.println(Math.abs(-9));//绝对值
System.out.println(Math.pow(2,4));//2^4
System.out.println(Math.ceil(-3.001));//取整 要>=该参数的最小整数
System.out.println(Math.ceil(3.002));//ceiling天花板
System.out.println(Math.floor(3.02));//向下取整
System.out.println(Math.round(3.6));//四舍五入
System.out.println(Math.sqrt(7));//开方
System.out.println(Math.random());//0-1随机数
System.out.println(Math.random()*5+2);//2-7小数 *b-a+1 + +a
System.out.println((int)(Math.random()*5+2));//2-6整数
System.out.println(Math.max(2,9));//最大值
}
}
Arrays类
package chapter16.Arrays类;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayMethod01 {
public static void main(String[] args) {
Integer []integers={1,20,90};
System.out.println(Arrays.toString(integers));//输出数组
Integer arr[]={1,-1,7,8,89};
Arrays.sort(arr);//默认排序
System.out.println("排序后==========");
System.out.println(Arrays.toString(arr));
Arrays.sort(arr, new Comparator() {//调用定制排序时,传入参数:排序数组arr 实现了Comparator接口匿名内部类 实现compare方法
@Override
public int compare(Object o1, Object o2) {
Integer i1=(Integer) o1;
Integer i2=(Integer) o2;
return i2-i1;//i1-i2从小到大
}
});
System.out.println(Arrays.toString(arr));
}
}
package chapter16.Arrays类;
import java.util.Arrays;
import java.util.List;
public class ArrayMethod02 {
public static void main(String[] args) {
int []arr={1,2,90,123,567};//二分搜索法进行查找,要求必须排好
int index = Arrays.binarySearch(arr, 124);//二叉查找索引,不存在则返回 -(应该排的位置+1)
System.out.println(index);
int []newArr=Arrays.copyOf(arr,arr.length+1);//从arr数组中拷贝arr.length个元素到 newArr 中
System.out.println("==拷贝执行完后==");
System.out.println(Arrays.toString(newArr));//如果拷贝长度长了,则int类+0 integer+null
Integer[] num=new Integer[]{9,3,2};
Arrays.fill(num,99);//用99去填充所有数组
System.out.println("数组填充后");
System.out.println(Arrays.toString(num));
Integer[]arr2={99,99,99};
System.out.println(Arrays.equals(arr2,num));//判断数组相同
ListasList=Arrays.asList(2,3,4,5,6,1);//将数据转成List集合 返回
System.out.println(asList);
System.out.println("asList运行类型"+asList.getClass());//Arrays$ArrayList 内部类
}
}
package chapter16.Arrays类;
import java.util.Arrays;
import java.util.Comparator;
public class ArraySortCustom {
public static void main(String[] args) {
int []arr={1,-1,8,0,20};
// bubble01(arr);
// System.out.println(Arrays.toString(arr));
bubble02(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int i1 = (Integer) o1;
int i2 = (Integer) o2;
return i1-i2;//返回的值大于0还是小于0决定是否交换
}
});
System.out.println(Arrays.toString(arr));
}
public static void bubble01(int []arr){
int temp=0;
for (int i = 0; i < arr.length-1 ; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if (arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
public static void bubble02(int[] arr, Comparator c){
int temp=0;
for (int i = 0; i < arr.length-1 ; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
//数组排序由 compare 返回值决定
if (c.compare(arr[j],arr[j+1])>0){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
}
package chapter16.Arrays类;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayExercise {
public static void main(String[] args) {
Book []books=new Book[4];
books[0]=new Book("xxx",100);
books[1]=new Book("yy",90);
books[2]=new Book("zzljj",5);
books[3]=new Book("ll",300);
Book.bubble(books, new Comparator() {
@Override
public int compare(Object o1, Object o2) {//返回int类 可用if else转
double i1=(Double) o1;
double i2=(Double) o2;
if (i1-i2>0){
return 1;
}else {
return -1;
}
}
});
Arrays.sort(books, new Comparator() {//直接用Arrays.sort
@Override
public int compare(Object o1, Object o2) {
Book book1=(Book)o1;
Book book2=(Book)o2;
return book2.getName().length()-book1.getName().length();
}
});
System.out.println(Arrays.toString(books));
}
}
class Book{
private String name;
private double price;
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
public static void bubble(Book[] books, Comparator c){
Book temp=null;
for (int i = 0; i < books.length-1 ; i++) {
for (int j = 0; j < books.length-i-1 ; j++) {
if (c.compare(books[j].price,(books[j+1].price))>0){
temp=books[j];
books[j]=books[j+1];
books[j+1]=temp;
}
}
}
for (int i = 0; i < books.length ; i++) {
System.out.println(books[i].toString());
}
}
}
System类
package chapter16.System类;
public class System01 {
public static void main(String[] args) {
System.out.println("1");
System.exit(0);//表示退出,0表示正常状态
System.out.println("2");
}
}
package chapter16.System类;
import java.util.Arrays;
public class System02 {
public static void main(String[] args) {
int []src={1,2,3};
int []dest=new int[3];// 0 0 0
System.arraycopy(src,0,dest,1,2);//从原数组第0个位置拷贝,到目标数组第1个开始,拷贝2个
//常用Arrays.copyOf() 复制数组
System.out.println(Arrays.toString(dest));
System.out.println(System.currentTimeMillis());//返回距离1970-1-1 毫秒数
}
}
大数处理
package chapter16.大数处理;
import java.math.BigInteger;
public class BigInteger01 {
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("888888888888888888");
BigInteger bigInteger1 = new BigInteger("6666666666666");
System.out.println(bigInteger);
BigInteger add=bigInteger.add(bigInteger1);//大数加减乘除要用相应方法
System.out.println(add);
BigInteger subtract = bigInteger.subtract(bigInteger1);
System.out.println(subtract);
BigInteger multiply = bigInteger.multiply(bigInteger1);
System.out.println(multiply);
BigInteger divide = bigInteger.divide(bigInteger1);
System.out.println(divide);
}
}
package chapter16.大数处理;
import java.math.BigDecimal;
public class BigDecimal01 {
public static void main(String[] args) {
BigDecimal bigDecimal = new BigDecimal("4849.15654185416541651541");
BigDecimal bigDecimal1 = new BigDecimal("26.2165515616");
System.out.println(bigDecimal);
System.out.println(bigDecimal.multiply(bigDecimal1));//加减乘除使用相应方法
System.out.println(bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING));//除不尽保留分子精度
}
}
Date类
package chapter16.Date类;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Date01 {
public static void main(String[] args) throws ParseException {
Date d1 = new Date();//获取当前系统时间
System.out.println(d1);//输出国外格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");//创建格式
String format =simpleDateFormat.format(d1);
System.out.println(format);
System.out.println(new Date(156146516));//通过毫秒数指定时间
String s="2022年04月11日 09:30:07 星期一";
Date parse=simpleDateFormat.parse(s);//把String转成对应Date,按国外格式 使用格式要和给定格式一致
System.out.println(parse);
}
}
日历类
package chapter16.日历类;
import java.util.Calendar;
public class Calendar01 {
public static void main(String[] args) {
/*
1.Calender是一个抽象类,构造器是private
2.可通过getInstance()来获取实例
3.提供大量方法和字段供使用
4.没有提供对应格式化类,需要自行组合输出
5.Calendar.HOUR > Calendar.HOUR_OF_DAY 24小时进制
*/
Calendar c = Calendar.getInstance();
System.out.println(c);
//获取日历对象某个日历字段
System.out.println("年:"+c.get(Calendar.YEAR));
System.out.println("月:"+(c.get(Calendar.MONTH)+1));//按0开始算
System.out.println("日:"+c.get(Calendar.DAY_OF_MONTH));
System.out.println("小时:"+c.get(Calendar.HOUR));
System.out.println("分钟:"+c.get(Calendar.MINUTE));
System.out.println("秒:"+c.get(Calendar.SECOND));
System.out.println(c.get(Calendar.HOUR_OF_DAY));// 24小时进制
}
}
package chapter16.日历类;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class LocalDate01 {
public static void main(String[] args) {
//第三代日期
LocalDateTime now = LocalDateTime.now();//返回当前日期时间的对象
System.out.println(now);
System.out.println(now.getMonth());
System.out.println(now.getMonthValue());
LocalDate now1 = LocalDate.now();//年月日
System.out.println(now1.getMonth());//没有Hour 比前面功能少
LocalTime now2 = LocalTime.now();//时分秒
DateTimeFormatter d = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh小时mm分ss秒");
System.out.println(d.format(now));
LocalDateTime localDateTime = now.plusDays(890);
System.out.println("890天后"+d.format(localDateTime));
LocalDateTime localDateTime1 = now.minusMinutes(3456);
System.out.println("3456分前"+d.format(localDateTime1));
//其他方法查API使用
}
}
package chapter16.日历类;
import java.time.Instant;
import java.util.Date;
public class Instant01 {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now);
Date d = Date.from(now);//通过from 把Instant转成Date
System.out.println(d);
Instant instant = d.toInstant();
System.out.println(instant);//通过 d的toInstant转成Instant对象
}
}
章节作业
指定字符反转
package chapter16.章节作业.指定字符反转;
//abcdef>aedcbf
public class Homework01 {
public static void main(String[] args) {
String str="abcdef";
System.out.println(reverse(str,1,4));
try {
str=reverse(str,0,0);
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
}
public static String reverse(String str,int start,int end){
//对输入参数做验证
//技巧:写出正确的情况,取反
if (!(str!=null&&start>=0&&end>start&&end
注册处理
package chapter16.章节作业.注册处理;
public class Homework02 {
public static void main(String[] args) {
String name="jack";
String key="123456";
String email="[email protected]";
try {
userRegister(name,key,email);
System.out.println("注册成功!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void userRegister(String name,String key,String email){
if (!(name!=null&&key!=null&&email!=null)){
throw new RuntimeException("参数不能为空");
}
//过关
//第一关
int n=name.length();
if (!(n>=2&&n<=4)){
throw new RuntimeException("用户名长度要求为2-4");
}
//第二关
if (!(key.length()==6&&isDigital(key))){
throw new RuntimeException("密码长度为6且要为数字");
}
//第三关
int i =email.indexOf('@');
int j=email.indexOf('.');
if (!(i>0&&j>i)){
throw new RuntimeException("邮箱中要包含@和. 且@在.的前面");
}
}
public static boolean isDigital(String str){
char [] chars=str.toCharArray();
for (int i = 0; i < chars.length ; i++) {
if (chars[i] <'0'||chars[i]>'9') {
return false;
}
}
return true;
}
}
人名格式
package chapter16.章节作业.人名格式;
import java.util.Locale;
public class Homework03 {
public static void main(String[] args) {
String name="Xheng Zu T";
printName(name);
}
public static void printName(String str){
if (str==null){
System.out.println("str 不为空");
return;
}
String[] names=str.split(" ");//字符串以“ ”分割
if (names.length!=3){
System.out.println("输入的字符串格式不对");
return;
}
String format = String.format("%s,%s.%c", names[2], names[0], names[1].toUpperCase().charAt(0));//转大写取第一个
System.out.println(format);
}
}
字符统计
package chapter16.章节作业.字符统计;
public class Homework04 {
public static void main(String[] args) {
String str="gredNI UO2#5";
countStr(str);
}
public static void countStr(String str){
if (str==null){
System.out.println("输入为空");
}
int count=0;
int lowerCount=0;
int upperCount=0;
int otherCount=0;
for (int i = 0; i ='0'&&str.charAt(i)<='9'){
count++;
}else if (str.charAt(i)>='a'&&str.charAt(i)<='z'){
lowerCount++;
}else if (str.charAt(i)>='A'&&str.charAt(i)<='Z'){
upperCount++;
}else {
otherCount++;
}
}
System.out.println("数字有:"+count);
System.out.println("小写有:"+lowerCount);
System.out.println("大写有:"+upperCount);
System.out.println("其他有:"+otherCount);
}
}