Java基础练习---API练习

@Test
	public void test1(){
		//public String():创建一个空白字符串对象,不含有任何内容
		String s1 = new String();
		System.out.println("s1:" + s1);//s1:
		
		//public String(char[] chs):根据字符数组的内容,来创建字符串对象
		char[] chs = {'a', 'b', 'c'};
		String s2 = new String(chs);
		System.out.println("s2:" + s2);//s2:abc
		
		//public String(byte[] bys):根据字节数组的内容,来创建字符串对象
		byte[] bys = {97, 98, 99};
		String s3 = new String(bys);
		System.out.println("s3:" + s3);//s3:abc
		
		//String s = “abc”;	直接赋值的方式创建字符串对象,内容就是abc
		String s4 = "abc";
		System.out.println("s4:" + s4);//s4:abc
	}
@Test
	public void test2(){
		String str = "北京,上海,杭州,深圳";
		String[] arr = str.split(",");
		for(String s:arr){
			System.out.println(s);
		}
	}
@Test
	public void test3(){
		String str = "helloJavaWorld";
		int a1 =str.indexOf("world");
		int a2 =str.indexOf("po");
		int a3 =str.indexOf("World");
		System.out.println("a1:"+a1);//-1
		System.out.println("a2:"+a2);//-1
		System.out.println("a3:"+a3);//9
	}
/*
北京
上海
杭州
深圳
*/
@Test
	public void test3(){
		String str = "helloJavaWorld";
		int a1 =str.indexOf("world");
		int a2 =str.indexOf("po");
		int a3 =str.indexOf("World");
		System.out.println("a1:"+a1);//-1
		System.out.println("a2:"+a2);//-1
		System.out.println("a3:"+a3);//9
	}
@Test
	public void test4(){
		String str = "yaghasdhghj@qq.com";
		String str2 = str.replace("qq", "163");
		System.out.println(str2);//yaghasdhghj@163.com
	}
@Test
	public void test5(){
		String s = " hjs iajhk  ";
		System.out.println(s);// hjs iajhk  
		System.out.println(s.trim());//hjs iajhk
	}
@Test
	public void test6(){
		//public StringBuilder():创建一个空白可变字符串对象,不含有任何内容
        StringBuilder sb = new StringBuilder();
        System.out.println("sb:" + sb);
        System.out.println("sb.length():" + sb.length());

        //public StringBuilder(String str):根据字符串的内容,来创建可变字符串对象
        StringBuilder sb2 = new StringBuilder("hello");
        System.out.println("sb2:" + sb2);
        System.out.println("sb2.length():" + sb2.length());
	}
@Test
	public void test7(){
		//创建对象
        StringBuilder sb = new StringBuilder();

        //public StringBuilder append(任意类型):添加数据,并返回对象本身
//        StringBuilder sb2 = sb.append("hello");
//
//        System.out.println("sb:" + sb);
//        System.out.println("sb2:" + sb2);
//        System.out.println(sb == sb2);

//        sb.append("hello");
//        sb.append("world");
//        sb.append("java");
//        sb.append(100);

        //链式编程
        sb.append("hello").append("world").append("java").append(100);

        System.out.println("sb:" + sb);

        //public StringBuilder reverse():返回相反的字符序列
        sb.reverse();
        System.out.println("sb:" + sb);
	}
@Test
	public void test8(){
		String str = "HelloJavaWorld"; 
		System.out.println(str.substring(5, 9));//java
		System.out.println(str.substring(9, 14));//World
		System.out.println(str.substring(9));//World
	}
@Test
	public void test9(){
		/*
		 
一。字符串 String str = "...  我爱北京烤鸭店,焖炉挂炉尝个遍";
1.创建新的String类子字符串,内容为str去掉"..." 输出新字符串
2.对str字符串以","为节点分开 创建字符串数组 遍历字符串数组并在控制台输出
3.找出字符串中"爱"字第一次出现的位置
4.对str进行操作,把"..."替换成"歌词:" 并输出新的字符串
5.把str中所有的"烤鸭店"都替换成"北冰洋" 并输出新的字符串语句
6.对str进行操作,去掉空格 并输出新的字符串语句 
		 */
		
		String str = "...  我爱北京烤鸭店,焖炉挂炉尝个遍";
		String str1 = str.substring(3);
		System.out.println(str1);
		
		String[] arr = str.split(",");
		for(String s : arr){
			System.out.println(s);
		}
		System.out.println(str.indexOf("爱"));
		
		String str2 = str.replace("...", "歌词:");
		System.out.println(str2);
		
		String str3 = str.replace("烤鸭店", "北冰洋");
		System.out.println(str3);
		
		String str4 = str.replace(" ", "");
		System.out.println(str4);
	}
@Test
	public void test10(){
		 // 获取开始的时间节点
        long start = System.currentTimeMillis();
        for (int i = 1; i <= 10000; i++) {
            System.out.println(i);
        }
        // 获取代码运行结束后的时间节点
        long end = System.currentTimeMillis();
        System.out.println("共耗时:" + (end - start) + "毫秒");
	}
@Test
	public void test11(){
		StringBuffer str = new StringBuffer("HelloWorld");
		str.insert(5, "java");
		System.out.println(str);//HellojavaWorld
		str.delete(5, 9);
		System.out.println(str);//HelloWorld
		str.replace(5, 10, "java");
		System.out.println(str);//Hellojava
		String s = str.substring(5,9);
		System.out.println(s);//java

	}
@Test
	public void test1(){
		StringBuffer sb = new StringBuffer("hello");
		System.out.println(sb.getClass());
		//基本上每一个对象的hash值都不一样,获取哈希值,是一个整数型
		System.out.println(sb.hashCode());
	}
@Test
	public void test2(){
		 //public Date():分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
        Date d1 = new Date();
        System.out.println(d1);//Thu Aug 19 15:40:36 CST 2021

        //public Date(long date):分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
        long date = 1000*60*60;
        Date d2 = new Date(date);
        System.out.println(d2);//Thu Jan 01 09:00:00 CST 1970
	}
@Test
	public void test3(){
		//创建日期对象
        Date d = new Date();

        //public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
        System.out.println(d.getTime());//1629359232841
        System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");//51.666642340214366年

        //public void setTime(long time):设置时间,给的是毫秒值
//        long time = 1000*60*60;
        long time = System.currentTimeMillis();
        d.setTime(time);//Thu Aug 19 15:47:12 CST 2021

        System.out.println(d);
	}
@Test
	public void test4() throws Exception{
		//格式化:从 Date 到 String
        Date d = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String s = sdf.format(d);
        System.out.println(s);//2021年08月19日 16:08:06
        System.out.println("--------");

        //从 String 到 Date
        String ss = "2048-08-09 11:11:11";
        //ParseException
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dd = sdf2.parse(ss);
        System.out.println(dd);//Sun Aug 09 11:11:11 CST 2048
	}
@Test
	public void test5(){
		//获取日历类对象
        Calendar c = Calendar.getInstance();

        //public int get(int field):返回给定日历字段的值
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");//2021年8月19日

        //public abstract void add(int field, int amount):根据日历的规则,将指定的时间量添加或减去给定的日历字段
        //需求1:3年前的今天
//        c.add(Calendar.YEAR,-3);
//        year = c.get(Calendar.YEAR);
//        month = c.get(Calendar.MONTH) + 1;
//        date = c.get(Calendar.DATE);
//        System.out.println(year + "年" + month + "月" + date + "日");

        //需求2:10年后的10天前
//        c.add(Calendar.YEAR,10);
//        c.add(Calendar.DATE,-10);
//        year = c.get(Calendar.YEAR);
//        month = c.get(Calendar.MONTH) + 1;
//        date = c.get(Calendar.DATE);
//        System.out.println(year + "年" + month + "月" + date + "日");

        //public final void set(int year,int month,int date):设置当前日历的年月日
        c.set(2050,10,10);
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH) + 1;
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");//2050年11月10日
	}
@Test
	public void test1(){
		Random r = new Random();
		for(int i=0;i<3;i++){
			int num = r.nextInt(10);
			System.out.println("number:"+num);
		}
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

?abc!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值