24-08-01Math类,Array类,System类,日期类
文章目录
由于常用类不涉及太多的代码逻辑,大多只是简单的方法调用,在我们有需要时现查即可,所以本文仅仅展示常用方法代码即效果截图.
Math类
Math类方法
Math类顾名思义,里面有着丰富的数学方法。
public class MathMethod {
public static void main(String[] args) {
int abs = Math.abs(-9);
System.out.println(abs);//计算绝对值
double pow =Math.pow(2,4);
System.out.println(pow);//计算幂
double ceil = Math.ceil(-3.0001);
System.out.println(ceil);//向上取整
double floor = Math.floor(-4.99999);
System.out.println(floor);//向下取整
double round = Math.round(3.5);
System.out.println(round);//4舍5入
double sqrt = Math.sqrt(9.0);
System.out.println(sqrt);//求根
int a = 2;
int b = 7;
for (int i = 0; i < 10; i++) {
System.out.println((int)(a+Math.random()*(b-a+1)));//生成随机数
}
int min = Math.min(1,9);
int max = Math.max(1,2);
System.out.println(min);//输出两个数的最小值
System.out.println(max);//输出两个数的最大值
}
}
输出为
9
16.0
-3.0
-5.0
4.0
3.0
7
3
6
2
4
5
3
5
7
4
1
2
Arrays类
Arrays的排序
public class ArraysSortCustom {
public static void main(String[] args) {
int[] arr = {2,3,2,5,9,3,5,2};
int[] arr1 = {2,3,2,5,9,3,5,2};
bubble(arr);
System.out.println(Arrays.toString(arr));
Bubble01(arr1, new Comparator(){
@Override
public int compare(Object o1, Object o2) {
int i1 = (Integer)o1;
int i2 = (Integer)o2;
return i1 - i2;
}
});
System.out.println(Arrays.toString(arr1));
}
//普通的冒泡排序
public static void bubble(int[] arr) {
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if(arr[j]>arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//使用冒泡排序+接口编程
public static void Bubble01(int[] arr, Comparator c) {
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if(c.compare(arr[j],arr[j+1])<0) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
package com.array;
import java.util.Arrays;
import java.util.Comparator;
/**
* @author 黄暄
* @version 1.0
*/
public class ArraysMethod01 {
public static void main(String[] args) {
//传统遍历数组的方法
Integer[] integers= {1,2,3};
for (int i = 0; i < integers.length; i++) {
System.out.println(integers[i]);
}
//直接使用Arrays.toString方法
System.out.println(Arrays.toString(integers));
//sort方法的使用
Integer[] integers1 = {4,2,5,3,8,5,7};
Integer[] integers2 = {4,2,5,3,8,5,7};
//自然排序
Arrays.sort(integers1);
System.out.println(integers1);
//定制排序
System.out.println();
Arrays.sort(integers2, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
return i2 - i1;
}
});
System.out.println(Arrays.toString(integers1));
}
}
Arrays的方法
public class ArraysMethod02 {
public static void main(String[] args) {
Integer[] arr = {1,2,3,4,5,6};
int index = Arrays.binarySearch(arr,3);
System.out.println(index);
int index1 = Arrays.binarySearch(arr,0);
System.out.println(index1);
Integer[] newArr = Arrays.copyOf(arr,arr.length);
System.out.println(Arrays.toString(newArr));
Integer[] arr1 = {1,2,3};
Arrays.fill(arr1,99);
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.equals(arr,arr1));
List aslist = Arrays.asList(1,2,3,4,5);
System.out.println(aslist);
System.out.println("aslist的运行类型为"+aslist.getClass());
}
}
Arrays的练习
public class ArrayExercise01 {
public static void main(String[] args) {
Book book = new Book("红楼梦",100);
Book book1 = new Book("金瓶梅新",90);
Book book2 = new Book("青年文摘",5);
Book book3 = new Book("java从入门到放弃",300);
Book[] books = {book,book1,book2,book3};
//使用冒泡
bubble01(books, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Integer i1 = (Integer)o1;
Integer i2 = (Integer)o2;
return i1-i2;
}
});
bubble02(books, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Integer i1 = (Integer)o1;
Integer i2 = (Integer)o2;
return i1-i2;
}
});
//不使用冒泡
Arrays.sort(books, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Book book1 = (Book)o1;
Book book2 = (Book)o2;
return book1.getPrice()-book2.getPrice();
}
});
Arrays.sort(books, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Book book1 = (Book)o1;
Book book2 = (Book)o2;
return book1.getName().length()-book2.getName().length();
}
});
System.out.println(Arrays.toString(books));
}
public static void bubble01(Book[] books,Comparator comparator){
int temp = 0;
for (int i = 0; i < books.length - 1 ; i++) {
for (int j = 0; j < books.length - 1 -i; j++) {
if(comparator.compare(books[j].getPrice(),books[j+1].getPrice())>0){
temp = books[j].getPrice();
books[j].setPrice(books[j+1].getPrice());
books[j+1].setPrice(temp);
}
}
}
}
public static void bubble02(Book[] books,Comparator comparator){
String temp;
for (int i = 0; i < books.length - 1 ; i++) {
for (int j = 0; j < books.length - 1 -i; j++) {
if(comparator.compare(books[j].getName().length(),books[j+1].getName().length())<0){
temp = books[j].getName();
books[j].setName(books[j+1].getName());
books[j+1].setName(temp);
}
}
}
}
}
class Book {
private String name;
private int price;
public Book(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
System类
下面是System的一些方法
public class SystemMethod {
public static void main(String[] args) {
System.out.println("hx");
//System.exit(0);//退出系统
System.out.println("yyds");
int[] arr1 = {1,2,3};
int[] ints = new int[3];
System.arraycopy(arr1,0,ints,0,arr1.length);
System.out.println(Arrays.toString(ints));
System.out.println(System.currentTimeMillis());
}
}
日期类
第一代日期类
第一代日期类为Date
public class Date01 {
public static void main(String[] args) throws ParseException {
//输出当前时间
Date date = new Date();
System.out.println(date);
//中国格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
String s = simpleDateFormat.format(date);
System.out.println(s);
//毫秒表示
Date date1 = new Date(16826256);
System.out.println(date1);
String s1 = "2005年9月25日 10:20:30 星期一";
Date parse = simpleDateFormat.parse(s1);
System.out.println(simpleDateFormat.format(parse));
}
}
第二代日期类
第二代日期类为Calendar类
public class Date02 {
public static void main(String[] args) {
Calendar instance = Calendar.getInstance();
System.out.println(instance);
System.out.println(instance.get(Calendar.YEAR));
System.out.println(instance.get(Calendar.MONTH)+1);
System.out.println(instance.get(Calendar.DAY_OF_MONTH));
System.out.println(instance.get(Calendar.HOUR_OF_DAY));
System.out.println(instance.get(Calendar.MINUTE));
System.out.println(instance.get(Calendar.SECOND));
}
}
第三代日期类
第三代日期类为LocalDateTime,LocalTime,LocalDate类
public class Date03 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println(now.getYear());
System.out.println(now.getMonthValue());
System.out.println(now.getDayOfMonth());
System.out.println(now.getDayOfWeek());
System.out.println(now.getDayOfYear());
System.out.println(now.getHour());
System.out.println(now.getMinute());
System.out.println(now.getSecond());
LocalDate now1 = LocalDate.now();
System.out.println(now1./*getHour*/getYear());
//格式化
DateTimeFormatter d = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH小时mm分钟ss秒");
String s = d.format(now);
System.out.println(s);
LocalDate now2 = LocalDate.now();//获取当前年月日
LocalDateTime now3 = LocalDateTime.now();//获取当前时分秒
LocalDateTime localDateTime = now.plusDays(76326);
System.out.println(localDateTime);
LocalDateTime localDateTime1 = now.plusMinutes(73277);
System.out.println(localDateTime1);
}
}
Instant时间戳类
时间戳与Date的转换
public class Instant_ {
public static void main(String[] args) {
Instant now = Instant.now();
java.util.Date date = new java.util.Date();
//Instant->Date
java.util.Date from = Date.from(now);
System.out.println(from);
//Date->Instant
Instant instant = date.toInstant();
System.out.println(instant);
}
}
常用类练习
Homework01.java
public class Homework01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
String reserve = null;
try {
reserve = reserve(next, 1, 4);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println(reserve);
}
public static String reserve(String str,int star,int end) {
if(str != null && star >=0 && end > star && end < str.length()) {
throw new RuntimeException("参数不正确");
}
char temp;
char[] chars = str.toCharArray();
int left = star;
int right = end;
while(left<right) {
temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
}
Homework02.java
public class Homework02 {
public static void main(String[] args) {
input();
}
public static void input() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String next = scanner.next();
System.out.println("请输入密码");
String next1 = scanner.next();
System.out.println("请输入邮箱");
String next2 = scanner.next();
check(next,next1,next2);
}
public static void check(String user, String password, String email) {
if(!(user.length()>=2&&user.length()<=4)) {
throw new RuntimeException("用户名错误");
}
else if (!(password.length() == 6 && isDigital(password))) {
throw new RuntimeException("密码格式错误");
}
else if(!emailcheck(email)) {
throw new RuntimeException("邮箱格式错误");
}
}
public static boolean isDigital(String str) {
char[] chars = str.toCharArray();
for (char aChar : chars) {
if (!Character.isDigit(aChar))
return false;
}
return true;
}
public static boolean emailcheck(String email) {
int i = email.indexOf('@');
int j = email.indexOf('.');
if(!(i != -1 && i < j)) {
return false;
}
return true;
}
}
class User {
private String username,password,email;
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
}
HomeWork03.java
public class Homework03 {
public static void main(String[] args) {
String str = "Huang Xuan HHH";
pinjie(str);
}
public static void pinjie(String s) {
if(s == null) {
System.out.println("输入不能为空");
return;
}
String[] s1 = s.split(" ");
if(s1.length != 3) {
System.out.println("长度不等于三");
return;
}
String format = String.format("%s,%s .%c", s1[2], s1[0], s1[1].toUpperCase().charAt(0));
System.out.println(format);
}
}
Homework04.java
public class Homework04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
char[] charArray = next.toCharArray();
int a=0,b=0,c=0;
for (int i = 0; i < charArray.length; i++) {
if(Character.isDigit(charArray[i])) a++;
if(Character.isUpperCase(charArray[i])) b++;
if(Character.isLowerCase(charArray[i])) c++;
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
欢迎补充。。。