Java学习打卡Day22

知识总结

String

  1. 概念:字符串是常量,创建之后不可改变,字符串字面值存储在字符串池中,可以共享。
    String = “Hello”;产生一个对象,字符串池中存储。
    String s = new String(“Hello”);产生两个对象,堆、池各存储一个。
  2. 常用方法
    public char charAt(int index):根据下标获取字符
    public boolean contains(String str):判断当前字符串中是否含有str
    public char[] toCharArray():将字符串转换成数组
    public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标;不存在,则返回值-1
    public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引
    public int length():返回字符串的长度
    public String trim():去掉字符串的前后空格
    public String toUpperCase():将小写转成大写
    public Boolean endWith(String str):判断字符串是否以str结尾
    public String replace (char oldChar,char newChar):将旧字符替换成新字符
    public String[] split(String str):根据str做拆分

BigDecimal

double 是近似存储

  1. BigDecimal:位置: java.math包中;作用:精确计算浮点数
  2. 创建方式:BigDecimal bd = new BigDecimal(“1.0”);
    方法方式:
    BigDecimal add(BigDecimal bd) 加
    BigDecimal subtract(BigDecimal bd) 减
    BigDecimal multiply(BigDecimal bd) 乘
    BigDecimal divide(BigDecimal bd) 除
    除法:BigDecimal(BigDecimal bd,int scal,RoundingMode mode)
    参数scal: 指定精确到小数点后几位
    参数mode:指定小数部分的取舍模式,通常采取四舍五入的模式
    取值为BigDecimal.ROUND_HALF_UP

今日练习

  1. 从命令行上读取一个字符串,用两种不同的方法,把该字符串转换为一个int类型
    方法一:把String直接转换为int
    方法二:把String转换为Integer,再把Integer转换为int类型
import java.util.Scanner;

public class TestT7 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入一个数组:");
		String str = input.next();
		
		//String直接转换为int
		int i = Integer.parseInt(str);
		
		//String转换为Integer
		Integer j = Integer.valueOf(str);
		
		//Integer转换为int
		int k = j;
		
	}	
}

  1. 有下面代码
class Student{
	private int age;
	private String name;
	public Student(){}
	public Student(String name , int age){
		this.name = name;
		this.age = age;
	}
	public String toString(){
		return name + " " + age;
	}
}

public class TestStudent {

	public static void main(String[] args) {
		Student stu1 = new Student("tom",18);
		System.out.println(/*1*/);
	}
}

问:在/1/位置,填写什么代码能够编译通过?
A. stu1 + " " + 100
B. 100 + " " + stu1
C. " " + 100 + stu1
D. stu1 + 100 + " "
答:ABC

  1. 获取邮箱“[email protected]”中的用户名“zhengcg”.
    答:
public class TestGetEmailName {

	public static void main(String[] args) {
		String str = "[email protected]";
		int i = str.indexOf("@");
		char[] chr = str.toCharArray();
		for(int j = 0 ; j < i ; j++){
			System.out.print(chr[j]);
		}
	}
}
  1. 验证邮箱“[email protected]”是否是一个合法的邮箱格式。
    提示:
    (1)邮箱必须是包含“@”和“.”
    (2)最后一个“.”的位置必须大于“@”的位置
public class TestEmail {

	public static void main(String[] args) {
		String str = "[email protected]";
		if(str.indexOf("@")!=(-1) && str.indexOf("@")!=(-1)){
			if(str.indexOf(".") > str.indexOf("@")){
				System.out.println(str + "是一个合法的邮箱格式");
			}
		}
	}
}
  1. 将随机获取的UUID中的“-”去掉。
    答:
public class TestUUID {

	public static void main(String[] args) {
		String str = java.util.UUID.randomUUID().toString();
		System.out.println(str);
		char[] chr = str.toCharArray();
		do{
			int i = str.indexOf("-");
			chr[i] = ' ';
			str = new String(chr);
		}while(str.indexOf("-") != (-1));
		for(int i = 0; i < chr.length ; i++){
			System.out.print(chr[i]);
		}
	}
}
  1. 在给定的字符串“ABCDEFGhijklmn1234567”中获取随机的4个字符,并使用StringBuilder拼接成字符串。
    答:
public class TestStringBuilder {

	public static void main(String[] args) {
		String str = "ABCDEFGhijklmn1234567";
		char[] chr1 = str.toCharArray(),chr2 = {' ',' ',' ',' '} ;
		java.util.Random random = new java.util.Random();
		for(int i = 0 ; i < 4 ; i++){
			int j = random.nextInt(chr1.length);
			chr2[i] = chr1[j];
		}
		System.out.println(new String(chr2));
	}

}
  1. 给定一个由数字组成的字符串
    如:“12395868923173478943890234092”;统计每个数字出现的次数。
    答:
public class TestNumber {

	public static void main(String[] args) {
		int j = 0;
		String str = "12395868923173478943890234092";
		char[] chr = str.toCharArray();
		for(int a = 0 ; a < 10 ; a++,j = 0){
		do{
			int i = str.indexOf(String.valueOf(a));
			chr[i] = (char) (33 + a);
			str = new String(chr);
			j++;
		}while(str.indexOf(String.valueOf(a)) != (-1));
		System.out.println(a + "出现的次数:"+ j);
		}
		for(int i = 0; i < chr.length ; i++){
			System.out.print(chr[i]);
		}
	}
}

打卡

越努力,越幸运
The more effort,the more lucky

发布了33 篇原创文章 · 获赞 3 · 访问量 913

猜你喜欢

转载自blog.csdn.net/qq_44952731/article/details/104639273