“”"
original author: jacky Li
Email : 3435673055@qq.com
Last edited: 2022.11.9
“”"
目录

头歌上有关java作业:java常用类
第1关:String类
任务描述
本关任务:熟悉String类的基本使用
相关知识
1. 概述
字符串是由多个字符组成的一串数据(字符序列),字符串可以看成是字符数组。
在实际开发中,字符串的操作是最常见的操作,没有之一。而Java没有内置的字符串类型,所以,就在Java类库中提供了一个类String 供我们来使用。String 类代表字符串。
在应用程序中经常会用到字符串,所谓字符串就是指一连串的字符,它是由许多单个字符连接而成的,如多个英文字母所组成的一个英文单词。字符串中可以包含任意字符,这些字符必须包含在一对双引号“”之内,例如“abc”。在Java中定义了String和StringBuffer两个类来封装字符串,并提供了一系列操作字符串的方法,它们都位于java.lang包中,因此不需要导包就可以直接使用。
2. String类的特点
字符串是常量,它的值在创建之后不能更改
Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
字符串如果是变量相加,先开空间,在拼接。
字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
package cn.itcast_02;/** 字符串的特点:一旦被赋值,就不能改变。*/public class StringDemo {public static void main(String[] args) {String s = "hello";s += "world";System.out.println("s:" + s); // helloworld}}
字符串在内存中2
String s1 = new String(“hello”);和String s2 = “hello”;有什么区别?
前者创建了2个或1个对象,后者创建了1个或0个对象。
String s1 = new String(“hello”);String s2 = “hello”;s1 == s2; // falses1.equals(s2); // trueString s3 = “hello”; String s4 = “world”; String s5 = “helloworld”;S5== s3 + s4 ; //fales5 == “hello” + ”world”; //true
字符串如果是变量相加,先开空间,再拼接。
字符串如果是常量相加,是先加,然后到字符串常量池中找,如果有就直接返回,否则就创建。
3. 常见操作方法
3.1 构造方法
| 方法 | 说明 |
|---|---|
| String() | 创建一个内容为空的字符串 |
| String(byte[]) | 根据指定的字节数组创建对象 |
| String(byte[],int,int) | 根据字节数组的一部分创建对象 |
| String(char[]) | 根据指定的字符数组创建对象 |
| String(char[],int,int) | 根据字符数组的一部分创建对象 |
| String(String) | 根据指定的字符串内容创建对象 |
| String(byte[] bytes, Charset charset) | 使用指定的编码构造字符串对象 |
3.2 判断功能
| 方法 | 说明 |
|---|---|
| equals() | 比较字符串的内容是否相等,区分大小写 |
| equalsIgnoreCase() | 比较字符串的内容是否相等,忽略大小写 |
| contains(String str) | 判断大字符串中是否包含小字符串 |
| startsWith() | 判断字符串是否以某个字符串开头 |
| endsWith() | 判断字符串是否以某个字符串结尾 |
| isEmpty() | 判断字符串是否为空 |
3.3 获取功能
| 方法 | 说明 |
|---|---|
| length() | 获取字符串长度 |
| charAt(int index) | 获取指定位置的字符 |
| indexOf(int ch) | 字符第一次出现的索引 |
| indexOf(String str) | 字符串第一次出现的索引 |
| indexOf(int ch,int fromIndex) | 字符从指定位置后第一次出现的索引 |
| indexOf(String str,int from) | 字符串从指定位置后第一次出现的索引 |
| lastIndexOf() | 字符串最后一次出现的索引 |
| subString(int start) | 从指定位置开始截取字符串 |
| subString(int start,int end) | 截取字符串,包左不包右 |
3.4 转换功能
| 方法 | 说明 |
|---|---|
| getBytes() | 把字符串转成字节数组 |
| getCharArray() | 把字符串转成字符数组 |
| valueOf(char[] chs) | 把字符数组转成字符串 |
| valueOf(int i) | 把int类型的数据转成字符串 |
| toLowerCase() | 把字符串转成小写 |
| toUpperCase() | 把字符串转成大写 |
| concat(String str) | 字符串拼接 |
3.5 其他功能
| 方法 | 说明 |
|---|---|
| replace(char old,char new) | 替换字符 |
| replace(String old,String new) | 替换字符串 |
| trim() | 去掉字符串两端空格 |
| compareTo() | 按字典顺序比较字符串 |
| compareToIngnoreCase() | 按字典顺序比较字符串,忽略大小写 |
| format() | 格式化字符串 |
编程要求
根据提示,在右侧编辑器补充代码,完成以下任务:
1.输出原字符串
2.求出字符串长度
3.字符串转换成相应的大/小写
4.截取指定位置的字符串(3-7)
5.字符串相加
测试说明
平台会对你编写的代码进行测试:
测试输入:heLLo,WOrld,NICE 预期输出: 平均值:44.0 最大值:91
测试输入:5,1,151,12,22,100; 预期输出: 原字符串为:heLLo,WOrld,NICE 字符串长度为:16 转换成大写字符串:HELLO,WORLD,NICE 转换成小写字符串:hello,world,nice 第3-7的字符串内容为:Lo,W 字符串相加后:heLLo,WOrld,NICE end
开始你的任务吧,祝你成功!
package step1;
import java.util.Scanner;
public class StringLearning {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String stringExample = scanner.next();
String endStr = " end";
// ---------------------Begin------------------------
System.out.print("原字符串为:"+stringExample+"\n");
System.out.print("字符串长度为:"+stringExample.length()+"\n");
System.out.print("转换成大写字符串:"+stringExample.toUpperCase()+"\n");
System.out.print("转换成小写字符串:"+stringExample.toLowerCase()+"\n");
System.out.print("第3-7的字符串内容为:"+stringExample.substring(3,7)+"\n");
System.out.print("字符串相加后:"+stringExample+endStr+"\n");
// ---------------------End------------------------
}
}
第2关:StringBuffer类
任务描述
本关任务:学习StringBuffer的使用,完成以下操作:
1.将stringExample转换成StringBuffer类
2.向转化后的StringBuffer增加字符串educode
3.删除5-8的字符串
4.将7-13的字符串替换成world
5.截取1-10的字符串并输出
6.反转字符串并输出
相关知识
StringBuffer概念
由于字符串是常量,因此一旦创建,其内容和长度是不可改变的。如果需要对一个字符串进行修改,则只能创建新的字符串。为了便于对字符串进行修改,在JDK中提供了一个StringBuffer类(也称字符串缓冲区)。StringBuffer类和String类最大的区别在于它的内容和长度都是可以改变的。StringBuffer类似一个字符容器,当在其中添加或删除字符时,并不会产生新的StringBuffer对象。
我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
- StringBuffer是线程安全的可变字符序列。
- StringBuffer和String的区别?
前者长度和内容可变,后者不可变。如果使用前者做字符串的拼接,不会浪费太多的资源。
String类表示的字符串是常量,一旦创建后,内容和长度都是无法改变的。而StringBuffer表示字符容器,其内容和长度可以随时修改。在操作字符串时,如果该字符串仅用于表示数据类型,则使用String类即可,但是如果需要对字符串中的字符进行增删操作,则使用StringBuffer类。
String类覆盖了Object类的equals()方法,而StringBuffer类没有覆盖Object类的equals()方法,具体示例如下:
String类对象可以用操作符“+”进行连接,而StringBuffer类对象之间不能,具体示例如下:
1. 常见操作方法
2. 构造方法和获取方法
package cn.itcast_01;/** StringBuffer:* 线程安全的可变字符串。** StringBuffer和String的区别?* 前者长度和内容可变,后者不可变。* 如果使用前者做字符串的拼接,不会浪费太多的资源。** StringBuffer的构造方法:* public StringBuffer():无参构造方法* public StringBuffer(int capacity):指定容量的字符串缓冲区对象* public StringBuffer(String str):指定字符串内容的字符串缓冲区对象** StringBuffer的获取方法:* public int capacity():返回当前容量。 理论值* public int length():返回长度(字符数)。 实际值*/public class StringBufferDemo {public static void main(String[] args) {// public StringBuffer():无参构造方法StringBuffer sb = new StringBuffer();System.out.println("sb:" + sb);System.out.println("sb.capacity():" + sb.capacity());System.out.println("sb.length():" + sb.length());System.out.println("--------------------------");// public StringBuffer(int capacity):指定容量的字符串缓冲区对象StringBuffer sb2 = new StringBuffer(50);System.out.println("sb2:" + sb2);System.out.println("sb2.capacity():" + sb2.capacity());System.out.println("sb2.length():" + sb2.length());System.out.println("--------------------------");// public StringBuffer(String str):指定字符串内容的字符串缓冲区对象StringBuffer sb3 = new StringBuffer("hello");System.out.println("sb3:" + sb3);System.out.println("sb3.capacity():" + sb3.capacity());System.out.println("sb3.length():" + sb3.length());}}
3. 添加功能
package cn.itcast_02;/** StringBuffer的添加功能:* public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身** public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身*/public class StringBufferDemo {public static void main(String[] args) {// 创建字符串缓冲区对象StringBuffer sb = new StringBuffer();// public StringBuffer append(String str)// StringBuffer sb2 = sb.append("hello");// System.out.println("sb:" + sb);// System.out.println("sb2:" + sb2);// System.out.println(sb == sb2); // true// 一步一步的添加数据// sb.append("hello");// sb.append(true);// sb.append(12);// sb.append(34.56);// 链式编程sb.append("hello").append(true).append(12).append(34.56);System.out.println("sb:" + sb);// public StringBuffer insert(int offset,String// str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身sb.insert(5, "world");System.out.println("sb:" + sb);}}
运行结果:
sb:hellotrue1234.56sb:helloworldtrue1234.56
4. 删除功能
package cn.itcast_03;/** StringBuffer的删除功能* public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身* public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身*/public class StringBufferDemo {public static void main(String[] args) {// 创建对象StringBuffer sb = new StringBuffer();// 添加功能sb.append("hello").append("world").append("java");System.out.println("sb:" + sb);// public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身// 需求:我要删除e这个字符,肿么办?// sb.deleteCharAt(1);// 需求:我要删除第一个l这个字符,肿么办?// sb.deleteCharAt(1);// public StringBuffer delete(int start,int// end):删除从指定位置开始指定位置结束的内容,并返回本身// 需求:我要删除world这个字符串,肿么办?// sb.delete(5, 10);// 需求:我要删除所有的数据sb.delete(0, sb.length());System.out.println("sb:" + sb);}}
运行结果:
sb:helloworldjavasb:
5. 替换功能
package cn.itcast_04;/** StringBuffer的替换功能:* public StringBuffer replace(int start,int end,String str):从start开始到end用str替换*/public class StringBufferDemo {public static void main(String[] args) {// 创建字符串缓冲区对象StringBuffer sb = new StringBuffer();// 添加数据sb.append("hello");sb.append("world");sb.append("java");System.out.println("sb:" + sb);// public StringBuffer replace(int start,int end,String// str):从start开始到end用str替换// 需求:我要把world这个数据替换为"节日快乐"sb.replace(5, 10, "节日快乐");System.out.println("sb:" + sb);}}
运行结果:
sb:helloworldjavasb:hello节日快乐java
6. 反转功能
package cn.itcast_05;/** StringBuffer的反转功能:* public StringBuffer reverse()*/public class StringBufferDemo {public static void main(String[] args) {// 创建字符串缓冲区对象StringBuffer sb = new StringBuffer();// 添加数据sb.append("霞青林爱我");System.out.println("sb:" + sb);// public StringBuffer reverse()sb.reverse();System.out.println("sb:" + sb);}}
运行结果:
sb:霞青林爱我sb:我爱林青霞
7. 截取功能
package cn.itcast_06;/** StringBuffer的截取功能:注意返回值类型不再是StringBuffer本身了* public String substring(int start)* public String substring(int start,int end)*/public class StringBufferDemo {public static void main(String[] args) {// 创建字符串缓冲区对象StringBuffer sb = new StringBuffer();// 添加元素sb.append("hello").append("world").append("java");System.out.println("sb:" + sb);// 截取功能// public String substring(int start)String s = sb.substring(5);System.out.println("s:" + s);System.out.println("sb:" + sb);// public String substring(int start,int end)String ss = sb.substring(5, 10);System.out.println("ss:" + ss);System.out.println("sb:" + sb);}}
运行结果:
sb:helloworldjavas:worldjavasb:helloworldjavass:worldsb:helloworldjava
8. String和StringBuffer的相互转换
package cn.itcast_07;/** 为什么我们要讲解类之间的转换:* A -- B的转换* 我们把A转换为B,其实是为了使用B的功能。* B -- A的转换* 我们可能要的结果是A类型,所以还得转回来。** String和StringBuffer的相互转换?*/public class StringBufferTest {public static void main(String[] args) {// String -- StringBufferString s = "hello";// 注意:不能把字符串的值直接赋值给StringBuffer// StringBuffer sb = "hello";// StringBuffer sb = s;// 方式1:通过构造方法StringBuffer sb = new StringBuffer(s);// 方式2:通过append()方法StringBuffer sb2 = new StringBuffer();sb2.append(s);System.out.println("sb:" + sb);System.out.println("sb2:" + sb2);System.out.println("---------------");// StringBuffer -- StringStringBuffer buffer = new StringBuffer("java");// String(StringBuffer buffer)// 方式1:通过构造方法String str = new String(buffer);// 方式2:通过toString()方法String str2 = buffer.toString();System.out.println("str:" + str);System.out.println("str2:" + str2);}}
运行结果:
sb:hellosb2:hello---------------str:javastr2:java
编程要求
根据提示,在右侧编辑器补充代码,完成以下任务:
1.将stringExample转换成StringBuffer类
2.向转化后的StringBuffer增加字符串educode
3.删除5-8的字符串
4.将7-13的字符串替换成world
5.截取1-10的字符串并输出
6.反转字符串并输出
测试说明
平台会对你编写的代码进行测试:
测试输入:hello,educode,ilovelearnig 预期输出: 添加数据后:hello,educode,ilovelearnigeducode 删除5-8的字符串后:helloucode,ilovelearnigeducode 替换7-13的字符串后:helloucworldovelearnigeducode 截取1-10的字符串为:elloucwor 反转后的字符串为:edocudeginraelevodlrowcuolleh
开始你的任务吧,祝你成功!
package step2;
import java.util.Scanner;
public class StringBufferLearning {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String stringExample = scanner.next();
// ---------------------Begin------------------------
StringBuffer stringBuffer = new StringBuffer(stringExample);
stringBuffer.append("educode");
System.out.println(String.format("添加数据后:%s", stringBuffer));
stringBuffer.delete(5, 8);
System.out.println(String.format("删除5-8的字符串后:%s", stringBuffer));
stringBuffer.replace(7, 13,"world");
System.out.println(String.format("替换7-13的字符串后:%s", stringBuffer));
System.out.println(String.format("截取1-10的字符串为:%s", stringBuffer.substring(1, 10)));
stringBuffer.reverse();
System.out.println(String.format("反转后的字符串为:%s", stringBuffer));
// ---------------------End------------------------
}
}
第3关:Math类
任务描述
本关任务:认识Math类的使用方法,完成以下任务:
1.求出变量value1的绝对值
2.求出value1的3次幂
3.求出value1的二次方根
4.求出value1的sin值
5.求出value1与value2中的较大者
相关知识
Math类基本知识
Math类是数学操作类,Math类提供了常用的一些数学函数,如:三角函数、对数、指数等。一个数学公式如果想用代码表示,则可以将其拆分然后套用Math类下的方法即可。
Math类中有两个静态常量PI和E,分别代表数学常量π和e。
Math.abs(12.3); //12.3 返回这个数的绝对值Math.abs(-12.3); //12.3Math.copySign(1.23, -12.3); //-1.23,返回第一个参数的量值和第二个参数的符号Math.copySign(-12.3, 1.23); //12.3Math.signum(x); //如果x大于0则返回1.0,小于0则返回-1.0,等于0则返回0Math.signum(12.3); //1.0Math.signum(-12.3); //-1.0Math.signum(0); //0.0//指数Math.exp(x); //e的x次幂Math.expm1(x); //e的x次幂 - 1Math.scalb(x, y); //x*(2的y次幂)Math.scalb(12.3, 3); //12.3*2³//取整Math.ceil(12.3); //返回最近的且大于这个数的整数13.0Math.ceil(-12.3); //-12.0Math.floor(12.3); //返回最近的且小于这个数的整数12.0Math.floor(-12.3); //-13.0//x和y平方和的二次方根Math.hypot(x, y); //√(x²+y²)//返回概述的二次方根Math.sqrt(x); //√(x) x的二次方根Math.sqrt(9); //3.0Math.sqrt(16); //4.0//返回该数的立方根Math.cbrt(27.0); //3Math.cbrt(-125.0); //-5//对数函数Math.log(e); //1 以e为底的对数Math.log10(100); //10 以10为底的对数Math.log1p(x); //Ln(x+ 1)//返回较大值和较小值Math.max(x, y); //返回x、y中较大的那个数Math.min(x, y); //返回x、y中较小的那个数//返回 x的y次幂Math.pow(x, y);Math.pow(2, 3); //即2³ 即返回:8//随机返回[0,1)之间的无符号double值Math.random();//返回最接近这个数的整数,如果刚好居中,则取偶数Math.rint(12.3); //12.0Math.rint(-12.3); //-12.0Math.rint(78.9); //79.0Math.rint(-78.9); //-79.0Math.rint(34.5); //34.0Math.rint(35.5); //36.0Math.round(12.3); //与rint相似,返回值为long//三角函数Math.sin(α); //sin(α)的值Math.cos(α); //cos(α)的值Math.tan(α); //tan(α)的值//求角Math.asin(x/z); //返回角度值[-π/2,π/2] arc sin(x/z)Math.acos(y/z); //返回角度值[0~π] arc cos(y/z)Math.atan(y/x); //返回角度值[-π/2,π/2]Math.atan2(y-y0, x-x0); //同上,返回经过点(x,y)与原点的的直线和经过点(x0,y0)与原点的直线之间所成的夹角Math.sinh(x); //双曲正弦函数sinh(x)=(exp(x) - exp(-x)) / 2.0;Math.cosh(x); //双曲余弦函数cosh(x)=(exp(x) + exp(-x)) / 2.0;Math.tanh(x); //tanh(x) = sinh(x) / cosh(x);//角度弧度互换 360°角=2π弧度Math.toDegrees(angrad); //角度转换成弧度,返回:angrad * 180d / PIMath.toRadians(angdeg); //弧度转换成角度,返回:angdeg / 180d * PIMath.PI
package cn.itcast.chapter05.example15;/*** Math类中比较常见的方法*/public class Example15 {public static void main(String[] args) {System.out.println("计算绝对值的结果: " + Math.abs(-1));System.out.println("求大于参数的最小整数: " + Math.ceil(5.6));System.out.println("求小于参数的最大整数: " + Math.floor(-4.2));System.out.println("对小数进行四舍五入后的结果: " + Math.round(-4.6));System.out.println("求两个数的较大值: " + Math.max(2.1, -2.1));System.out.println("求两个数的较小值: " + Math.min(2.1, -2.1));System.out.println("生成一个大于等于0.0小于1.0随机值: " + Math.random());}}
编程要求
根据提示,在右侧编辑器补充代码,完成以下任务:
1.求出变量value1的绝对值
2.求出value1的3次幂
3.求出value1的二次方根
4.求出value1的sin值
5.求出value1与value2中的较大者
测试说明
平台会对你编写的代码进行测试:
测试输入:
81
-9 预期输出: value1=81, value2=-9 value1的绝对值为:81 value1的3次幂为:531441.000000 value1的2次方根为:9.000000 value1的sin值为:-0.629888 value1与value2中的较大者为:81
开始你的任务吧,祝你成功!
package step3;
import java.util.Scanner;
public class MathClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int value1 = scanner.nextInt();
int value2 = scanner.nextInt();
// ---------------------Begin------------------------
/**
1.求出变量value1的绝对值
2.求出value1的3次幂
3.求出value1的二次方根
4.求出value1的sin值
5.求出value1与value2中的较大者
* */
System.out.println(String.format("value1=%d, value2=%d", value1, value2));
System.out.println(String.format("value1的绝对值为:%d", Math.abs(value1)));
System.out.println(String.format("value1的3次幂为:%f", Math.pow(value1, 3)));
System.out.println(String.format("value1的2次方根为:%f", Math.sqrt(value1)));
System.out.println(String.format("value1的sin值为:%f", Math.sin(value1)));
System.out.println(String.format("value1与value2中的较大者为:%d", Math.max(value1, value2)));
// ---------------------End------------------------
}
}
第4关:Random类
任务描述
本关任务:掌握Random类的使用方法,完成以下任务:
1.生成5个整数并打印输出
2.生成5个double类型并打印输出
tip:Random类的种子需要设置为2022
相关知识
Random类
在JDK的java.util包中有一个Random类,它可以在指定的取值范围内随机产生数字。在Random类中提供了两个构造方法,具体如下表所示。
表中列举了Random类的两个构造方法,其中第一个构造方法是无参的,通过它创建的Random实例对象每次使用的种子是随机的,因此每个对象所产生的随机数不同。如果希望创建的多个Random实例对象产生相同序列的随机数,则可以在创建对象时调用第二个构造方法,传入相同的种子即可。
相对于Math的random()方法而言,Random类提供了更多的方法来生成各种伪随机数,不仅可以生成整数类型的随机数,还可以生成浮点类型的随机数,表中列举了Random类中的常用方法。
表中,列出了Random类常用的方法,其中,Random类的nextDouble()方法返回的是0.0和1.0之间double类型的值,nextFloat()方法返回的是0.0和1.0之间float类型的值,nextInt(int n)返回的是0(包括)和指定值n(不包括)之间的值。
package cn.itcast.chapter05.example16;import java.util.Random;/*** 使用构造方法Random()产生随机数*/public class Example16 {public static void main(String args[]) {Random r = new Random(); // 不传入种子// 随机产生10个[0,100)之间的整数for (int x = 0; x < 10; x++) {System.out.println(r.nextInt(100));}}}
package cn.itcast.chapter05.example17;import java.util.Random;/*** 使用构造方法Random(long seed)产生随机数*/public class Example17 {public static void main(String args[]) {Random r = new Random(13); // 创建对象时传入种子// 随机产生10个[0,100)之间的整数for (int x = 0; x < 10; x++) {System.out.println(r.nextInt(100));}}}
package cn.itcast.chapter05.example18;import java.util.Random;/*** Random类中的常用方法*/public class Example18 {public static void main(String[] args) {Random r1 = new Random(); // 创建Random实例对象System.out.println("产生float类型随机数: " + r1.nextFloat());System.out.println("产生0~100之间int类型的随机数:" + r1.nextInt(100));System.out.println("产生double类型的随机数:" + r1.nextDouble());}}
编程要求
根据提示,在右侧编辑器补充代码,完成以下任务:
1.生成5个整数并打印输出
2.生成5个double类型并打印输出
tip:Random类的种子需要设置为2022
测试说明
平台会对你编写的代码进行测试:
无输入
开始你的任务吧,祝你成功!
package step4;
import java.util.Random;
public class RandomClass {
public static void main(String[] args) {
// ---------------------Begin------------------------
Random random = new Random(2022);
for (int i = 0; i < 5; i++) {
System.out.println(random.nextInt());
}
for (int i = 0; i < 5; i++) {
System.out.println(random.nextDouble());
}
// ---------------------End------------------------
}
}
第5关:知识回顾
任务描述
本关任务:对本章主要理论知识点进行回顾复习。
相关知识
为了完成本关任务,你需要对之前完成的关卡进行复习。
编程要求
根据相关知识,按照要求完成题目。
测试说明
平台会对你选择的答案进行判断,全对则通过测试。
开始你的任务吧,祝你成功!
