• Java-day15(Java常用类)


    Java常用类

    1.String

    public class test1 {
    	/*
    	 * String:代表不可变的字符序列,底层使用char[]存放
    	 * String是final的 
    	 * */
    		@Test
    		public void test() {
    			String str1 = "Java EE";
    			String str2= "Java EE";
    			
    			String str3 = new String("Java EE");
    			
    			String str4 = "Java EE" + "Android";       
    			String str5 = "Android";
    			String str6 = str1 + str5;
    			str5 = str5 + "Hadoop";
    			String str7 = str6.intern();
    			String str8 = "Java EEAndroid";
    			System.out.println(str1 == str2);//true
    			
    			System.out.println(str1 == str3);//false
    			System.out.println(str1.equals(str3));//true
    			
    			System.out.println(str4 == str6);//false
    			System.out.println(str4.equals(str6));//true
    			System.out.println(str4 == str7);//true
    			System.out.println(str4 == str8);//true
    		}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在内存中如何存放**在这里插入图片描述**

    常用方法

    需要熟练掌握

    import org.junit.Test;
    
    public class test1 {
    	//String类的基本用法
    	@Test
    	public void test1() {
    		String str1 = "abcdefgabcch";
    		String str2 = "abc";
    		String str3 = "cch";
    		
    		int length():返回字符串长度
    		System.out.println(str2.length());//3
    		
    		char charAt(index):返回在指定index位置的字符,index从0开始
    		System.out.println(str2.charAt(2));//c      
    		
    		boolean equals(Object obj):比较两个字符串是否相等
    		System.out.println(str2.equals(str1));//false
    		
    		//int compareTo(String str):返回参与比较的前后两个字符串的ASCII码的差值,如果首字符相同,则比较下一个字符,直到有不同的为止,返回该不同的字符的asc码差值。
    		System.out.println(str2.compareTo(str1));
    		
    		//int indexOf(s):返回字符串s在当前字符串中首次出现的位置,若没有返回-1
    		System.out.println(str1.indexOf(str2));
    		
    		//int lastIndexOf(s):返回字符串s在当前字符串中最后一次出现的位置,若没有返回-1
    		System.out.println(str1.lastIndexOf(str2));
    		
    		//int indexOf(s,int star):返回字符串s在当前字符串中star位置开始首次出现的位置,若没有返回-1
    		System.out.println(str1.indexOf(str2,3));
    		
    		//int lastIndexOf(s,int star):返回字符串s在当前字符串中star位置之前最后一次出现的位置,若没有返回-1
    		System.out.println(str1.lastIndexOf(str2,12));
    		
    		//boolean startsWith(String abc):判断当前字符串是否以abc开始
    		System.out.println(str1.startsWith("abc"));
    		
    		//boolean endsWith(String abc):判断当前字符串是否以abc结尾
    		System.out.println(str1.endsWith("abc"));//false
    		
    		//boolean regionMatches(int fisted,String other,int otnered,int length):判断当前字符串从fisted开始的字串与另一个other字符串从otnered开始,length长度的字串是否equals。
    		System.out.println(str1.regionMatches(9,str3,0,str3.length()));//true
    		
    		String substring(int substr):复制从当前字符串的substr位置开始到最后
    		String str4 = str1.substring(6);
    		System.out.println(str4);//gabcch
    		
    		//String substring(int substr,int end):复制从当前字符串的substr位置开始到end-1的位置结束(注意:范围是左闭右开)
    		String str5 = str1.substring(6,9);
    		System.out.println(str5);//gab
    		
    		//String replance(char OldChar,char newChar):将当前字符串中的某些旧字符换成新字符(原来的字符串无变化)
    		System.out.println(str1);
    		String str6 = str1.replace("def", "ggg");
    		System.out.println(str6);
    		
    		//String trim():将当前的字符串首尾空格去掉
    		String str7 = "  aa  cc   ";
    		String str8 = str7.trim();
    		System.out.println(str7);
    		System.out.println(str8);
    		
    		//String concat(String str):连接当前字符串与str
    		String str9 = str1.concat(str8);
    		System.out.println(str1);
    		System.out.println(str9);
    		
    		System.out.println();
    		String[] split(String str):将当前字符串按str方式进行切割
    		String str10 = "hello _ jhji kkjhi _ j*";
    		String[] strs = str10.split(" ");
    		for(int i = 0;i < strs.length;i++) {
    			System.out.print(strs[i]);
    		}		
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    练习
    题一:模拟trim方法,去除字符串两端的空格

    public class test2{
    	public static void main(String[] args) { 
    		String str1 = "  hello world ";
    		System.out.print(new StringDemo().myTrim(str1));
    	}
    }
    class StringDemo{
    	public  String myTrim(String str) {
    		int start = 0;
    		int end = str.length() - 1;
    		while(start < end && str.charAt(start) == ' ') {
    			start++;
    		}
    		while(start < end && str.charAt(end) == ' ') {
    			end--;
    		}
    		return str.substring(start,end + 1);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    题二:让一个字符串指定的位置反转

    public class test3{
    	public static void main(String[] args) {     
    		String str1 = "abcdefghijklmn";
    		test3 t = new test3();
    		System.out.println(t.reverseString(str1,1,4));
    	}
    	public static String reverseString(String str,int start,int end) {
    		char[] c = str.toCharArray();
    		return reverseArray(c,start,end);
    	}
    	public static String reverseArray(char[] c,int start,int end) {
    		for(int i = start,j = end;i < j;i++,j--) {
    			char temp = c[i];
    			c[i] = c[j];
    			c[j] = temp;
    		}
    		//字符数组转字符串
    		return new String(c);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    题三:获取一个字符串在另一个字符串中出现的次数

    public class test4{
    	public static void main(String[] args) {   
    		String str1 = "abdddcddefghddijklddmn";
    		String str2 = "dd";
    		test4 t = new test4();
    		System.out.println(t.getTime(str1,str2));
    	}
    	public int getTime(String str1,String str2) {
    		int count = 0;
    		int len;
    		while((len = str1.indexOf(str2)) != -1) {
    			count++;
    			str1 = str1.substring(len + str2.length());
    		}		
    		return count;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    题四:获取两个字符串中最大相同子串
    提示:将短的串进行长度依次递减的子串与较长的串进行比较

    public class test5{
    	public static void main(String[] args) {      
    		String str1 = "abdddcddefghddijklddmn";
    		String str2 = "dijkldsdfyuiddefghu";
    		test5 t = new test5();
    		System.out.println(t.getMaxString(str1,str2));
    	}
    	public List<String> getMaxString(String str1,String str2) {
    		String maxStr = (str1.length() > str2.length()) ? str1 : str2;
    		String minStr = (str1.length() < str2.length()) ? str1 : str2;
    		int len = minStr.length();
    		List<String> list = new ArrayList<String>();
    		for(int i = 0;i < len;i++) {
    			for(int x = 0,y = len - i;y <= len;x++,y++) {
    				String str = minStr.substring(x,y);
    				if(maxStr.contains(str)) {
    					list.add(str);
    				}
    			}
    			if(list.size() != 0) {
    				return list;
    			}
    		}
    		return null;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    题五:对字符数组进行自然排序
    提示:串变成数组;Arrays.sort();数组转串

    public class test6{
    	public static void main(String[] args) {       
    		String str1 = "hello Ande";
    		test6 t = new test6();
    		System.out.println(t.sort(str1));
    	}
    	public String sort(String str) {
    		char[] c = str.toCharArray();//字符串转数组
    		Arrays.sort(c);//数组排序
    		return new String(c);//数组转字符串
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    String与包装类(基本数据类型),字节数组的转换

    1.字符数组与基本数据类型,包装类的转换
    //字符串转基本数据类型,包装类:调用相应包装类的parseXxx(String str)方法
    //基本数据类型,包装类转字符串:调用字符串的重载的valueOf()方法
    2.字符串与字节数组的转换
    //字符串转字节数组:调用字符串的getBytes()方法
    //字节数组转字符串:调用字符串的构造器
    3.字符串与字符数组的转换
    //字符串转字符数组:调用字符串的toCharArray()方法
    //字符数组转字符串:调用字符串的构造器
    4.String类与StringBuffer类的转换
    //String类转换StringBuffer类:使用StringBuffer的构造器new StringBuffer(String str)  
    //StringBuffer类转换 String类:使用StringBuffertoString()方法

    public class test7 {
    
    	public static void main(String[] args) {                           
    		//1.字符数组与基本数据类型,包装类的转换
    		//字符串转基本数据类型,包装类:调用相应包装类的parseXxx(String str)方法
    		String str1 = "1234";
    		int i = Integer.parseInt(str1);
    		System.out.println(i);
    		
    		//基本数据类型,包装类转字符串:调用字符串的重载的valueOf()方法
    		//法一:
    		String str2 = i + "";
    		//法二:
    		str2 = String.valueOf(i);
    		System.out.println(str2);
    		System.out.println();
    		
    		//2.字符串与字节数组的转换
    		//字符串转字节数组:调用字符串的getBytes()方法
    		byte[] c = str2.getBytes();
    		for(int j = 0;j < c.length;j++) {
    			System.out.println((char)c[j]);
    		}
    		System.out.println();
    		
    		//字节数组转字符串:调用字符串的构造器
    		String str3 = new String(c);
    		System.out.println(str3);
    		System.out.println();
    		
    		//3.字符串与字符数组的转换
    		//字符串转字符数组:调用字符串的toCharArray()方法
    		String str4 = "你好,世界";
    		char[] c1 = str4.toCharArray();
    		for(int k = 0;k < c1.length;k++) {
    			System.out.println(c1[k]);
    		}
    		System.out.println();
    		
    		//字符数组转字符串:调用字符串的构造器
    		String str5 = new String(c1);
    		System.out.println(str5);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    2.StringBuffer

    java.lang.StringBuffer代表可变长度的字符序列,可以对字符串内容进行修改
    StringBuffer是一个容器
    在这里插入图片描述

    添加:append() 删除:delete()
    修改:setCharAt(int index,char ch)replace(int index1,int index2, String str)
    查:charAt() 插入:insert(int index,String str)
    反转:reverse() 长度:length()
    例:
    public class test8 {

    @Test
    public void test1() {
    	StringBuffer sb = new StringBuffer();
    	System.out.println(sb.length());
    	sb.append("assd").append("122234234");//添加
    	System.out.println(sb);
    	sb.insert(3, " hello ");//插入
    	System.out.println(sb);
    	sb.reverse();//反转
    	System.out.println(sb);
    	sb.delete(0, 5);//删除
    	System.out.println(sb);
    	char sb1 = sb.charAt(4);//查找索引处的字符
    	System.out.println(sb1);
    	sb.setCharAt(4,'g');//修改(单个字符)
    	System.out.println(sb);
    	sb.replace(1, 4, "123");//修改/取代(字符串)
    	System.out.println(sb);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    }

    3.StringBuilder

    java.lang.StringBuilder代表可变长度的字符序列,JDK5.0之后新加的特性
    线程不安全,效率要高于StringBuffer
    效率比较:StringBuilder > StringBuffer > String

    4.System类时间

    currentTimeMillis()用于计算时间差(当前时间与1970年1月1日0时0分0秒之间的时间差,毫秒为单位)

    public class test9 {
    
    	public static void main(String[] args) {        
    		System.out.println(System.currentTimeMillis());
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5.Date类(java.util.Date及子类java.sql.Date

    表示特定的瞬间,精确到毫秒
    构造方法:
    Date()无参数创建的对象获取的是当前时间

    常用方法:

    toString() ; getTime()  
    
    • 1

    例:

    public class test10 {
    	@Test
    	public void test1() {
    		Date d1 = new Date();
    		System.out.println(d1.toString());//Fri Sep 22 21:40:16 CST 2023 
    		System.out.println(d1.getTime());//1695390670262
    		Date d2 = new Date(1695390670262L);
    		System.out.println(d2.toString());
    //		java.sql.Date d2 = new java.sql.Date(6543213456L);
    //		System.out.println(d2.toString());//1970-03-18
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    6.SimpleDateFormat

    Date类的API不易于国际化,大部分被废弃,java.text.SimpleDateFormat类是不与语言环境有关的方式来格式化和分析日期的具体类
    它允许进行格式化(日期–>文本{SimpleDateFormat类的format()方法}),解析(文本 --> 日期{SimpleDateFormat类的parse方法})
    无参数构造器会用默认的模式和语言环境创建对象
    例:

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.junit.Test;
    
    public class test10 {
    	@Test
    	public void test2() throws ParseException {
    		//格式化1
    		SimpleDateFormat sdf = new SimpleDateFormat();
    		String date = sdf.format(new Date());        
    		System.out.println(date);//默认格式:23-9-22 下午10:08
    		//格式化2:按照指定的patten进行格式化
    		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    		String date1 = sdf1.format(new Date());
    		System.out.println(date1);//2023-09-22 10:16:40
    		
    		//3.解析
    		Date date2 = sdf.parse("23-9-22 下午10:08");
    		System.out.println(date2);//Fri Sep 22 22:08:00 CST 2023
    		
    		date2 = sdf1.parse("2023-09-22 10:16:40");
    		System.out.println(date2);//Fri Sep 22 10:16:40 CST 2023		
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    	//3天打鱼两天晒网(从1990-01-01开始,date2>date1)
    	public int getDays(String date1,String date2) throws ParseException {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    		Date d1 = sdf.parse(date1);
    		Date d2 = sdf.parse(date2);
    		long milliTime  = d2.getTime() - d1.getTime();
    		return (int)milliTime/1000/3600/24 + 1;
    	}
    	@Test
    	public void test3() throws ParseException {
    		String str1 = "1990-01-01";
    		String str2 = "1990-10-03";
    		int dates = getDays(str1,str2);
    		if(dates % 5 == 0 || dates % 5 == 4) {
    			System.out.println(str2 + "  渔夫在晒网");
    		}else {
    			System.out.println(str2 + "  渔夫在打鱼");
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    7.Calendar类(日历类)

    抽象基类,主要用于完成日期字段之间相互操作的功能
    获取实例:1.使用Calendar.getlnstance()方法;2.调用其子类GregorianCalendar的构造器
    在这里插入图片描述

    例:

    @Test
    public void test4() {
    Calendar c = Calendar.getInstance();
    int day = c.get(Calendar.DAY_OF_MONTH);
    System.out.println(day);//12 返回今天是这个月的第几天

    c.add(Calendar.DAY_OF_MONTH,2);
    day = c.get(Calendar.DAY_OF_MONTH);
    System.out.println(day);//14
    
    c.set(Calendar.DAY_OF_MONTH,29);
    Date d = c.getTime();
    System.out.println(d);//Fri Sep 29 23:10:10 CST 2023
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    }

    8.Math

    在这里插入图片描述

    9.BigInteger类与BigDecimal

    在这里插入图片描述

    在这里插入图片描述

    例:

    @Test
    	public void test5() {
    		BigInteger bi = new BigInteger("1234565432");
    		BigDecimal bd = new BigDecimal("12345.213");
    		BigDecimal bd2 = new BigDecimal("11");
    		
    		System.out.println(bi);
    		//System.out.println(bd.divide(bd2));
    		System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP));  
    		System.out.println(bd.divide(bd2,15,BigDecimal.ROUND_HALF_UP));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    感谢大家的支持,关注,评论,点赞!
    参考资料:
    尚硅谷宋红康20天搞定Java基础下部

  • 相关阅读:
    分析各大常用的JS加密优缺点
    【C++】数组中出现次数超过一半的数字
    Eclipse常用设置
    【Python】Python进阶
    自学Python只看这个够不够........?
    vue+echarts项目八:库存情况(显示占比圆环图)
    Qt实现json解析
    Linux网络编程3-select模型
    APP每次打开都需要登录
    Vim文档编辑器常用语法总结
  • 原文地址:https://blog.csdn.net/weixin_51202460/article/details/133190601