Java知识复习巩固DAY2

String类

概述

类String中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻译为大写或小写的所有字符的字符串的副本。

构造方法

public String():初始化新创建的String对象,以使其表示空字符序列。

public String(char[] value):通过当前参数中的字符数组来构造新的String。

public String(byte[] bytes):通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。

构造举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


public class DemoDay2String {
public static void main(String[] args) {
//第一种
String str1 = new String();
System.out.println(str1);

//第二种
char chars[] = {'A','B','C'};
String str2 = new String(chars);
System.out.println(str2);

//第三种
byte bytes[] = {98,99,100};
String str3 = new String(bytes);
System.out.println(str3);

//第四种
String str4 = "hello";
System.out.println(str4);
}
}

判断方法

1
2
public boolean equals(Object anObject)://将此字符串与指定对象进行比较。
public boolean equalsIgnoreCase(String anotherString)://将此字符串与指定对象进行比较,忽略大小写。

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


public class DemoDay2String02 {
public static void main(String[] args) {
String s1 = "xiaolaohu";
String s2 = "xiaolaohu";
String s3 = "XIAOlaohu";

System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//false

System.out.println(s1.equalsIgnoreCase(s2));//true
System.out.println(s1.equalsIgnoreCase(s3));//true
}
}

获取功能的方法

  • Public int length(): 获取此字符串的长度
  • public String concat(String str): 将指定的字符串连接到该字符串内的末尾
  • public char charAt(int index): 返回字符串指定索引的char值
  • public int indexOf(String str): 返回指定子字符串第一次出现在该字符串内的索引
  • public String substring(int beginIndex): 返回一个子字符串,从beginIndex(索引)开始街区字符串到字符串结尾
  • public String substring(int beginIndex,int endIndex):返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。

代码示例

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
public class DemoDay02String03 {

public static void main(String[] args) {
String str1 = "Hellword!";

//获取长度
System.out.println(str1.length());
//将指定字符添加到该字符串末尾
String str2 = str1.concat(" ly0n!");
System.out.println(str2);

//返回字符串指定索引到char值
int i = str1.charAt(3);
System.out.println(i);

//返回指定子字符串第一次出现在该字符串内的索引
String str3 = "l";
System.out.println(str1.indexOf(str3));

//返回一个子字符串,从beginIndex(索引)开始街区字符串到字符串结尾
String str4 = str1.substring(3);
System.out.println(str4);

//
String str5 = str1.substring(3,8);
System.out.println(str5);
}

}

转换功能的方法

  • Public char[] tocahrArray(): 将此字符串转换为新的字符数组
  • public byte[] getBytes(): 使用平台默认的字符集将该String编码转换为新的字节数组
  • public String replace(CharSequence target,CharSequence replacement): 将与target匹配的字符串使用replacement字符串替换。

CharSequence是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class StringDemo03 {
public static void main(String[] args) {
String str1 = "xiaolaohu";
//将此字符串转换为新的字符数组
char[] chars = str1.toCharArray();
for(int i=0;i<chars.length;i++){
System.out.println(chars[i]);
}
//使用平台默认的字符集将该String编码转换为新的字节数组
byte[] bytes = str1.getBytes();
for(int x=0;x< bytes.length;x++){
System.out.println(bytes[x]);
}
//将与target匹配的字符串使用replacement字符串替换
String str2 = str1.replace("xiao","mu");
System.out.println(str2);

}
}

分割功能的方法

  • public String[] spilt(String regex): 将此字符串按照给定的regex(规则)拆分为字符串数组。

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**

public String[] spilt(String regex): 将此字符串按照给定的regex(规则)拆分为字符串数组。
**/

public class StringDemo04 {
public static void main(String[] args) {
String str1 = "ly0n,da1sy,p1yang";
String[] Str2 = str1.split(",");
for(int i=0;i<Str2.length;i++){
System.out.println(Str2[i]);
}

}
}

String类练习

定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。格式参照如下:[word1#word2#word3]

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
/**
定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。
格式参照如下:[word1#word2#word3]

定义一个int数组
**/

public class StringTest01 {
public static void main(String[] args) {
int[] i ={1,2,3};

String s = IntToString(i);
System.out.println("s:"+s);
}
public static String IntToString(int[] i){
String s = new String("[");
for(int x=0;x<i.length;x++){
if(x==i.length-1){
s = s.concat(i[x]+"]");
}else{
s = s.concat(i[x]+"#");
}
}
return s ;
}
}

Static关键字

概述

可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不是单单是属于某个对象的。也就是说,既然属于类,就可以不靠创建对象来调用了。

定义和使用格式

当static修饰成员变量时,该变量称为类变量。该类的每个对象都共享同一个类变量的值。任何对象都可以更改该类变量的值,但也可以在不创建该类的对象的情况下对类变量进行操作。

  • 类变量:使用static修饰的成员变量

定义格式

static 数据类型 变量名;

示例

Static int i;

静态方法

当static修饰成员方法时,该方法称为类方法。静态方法在声明中有static关键字,可以直接使用类名来调用,不需要创建类的对象。调用方式简单。

类方法: 使用static修饰粉成员方法,常称为静态方法

定义格式

修饰符 static 返回值类型 方法名(参数列表){

​ //执行语句

}

举例:在Student类中定义静态方法 showNum

public static void showNum(){

​ System.out.println(“num”+number);

}

静态方法调用注意事项

静态方法可直接访问类变量和静态方法

静态方法不能直接访问普通成员变量会成员方法。但是成员方法可以直接访问类变量或者静态方法

静态方法只能够不能使用this关键字

静态方法只能访问看静态成员

调用格式

​ 代码示例

1
2
3
4
5
6
7
8
public class StuDemo2{
public static void main(String[] args){
//访问类变量
System.out.println(Student.numberOfStudent);
//调用静态方法
Student.showNum();
}
}

原理图解

stastic修饰的内容

随着类的还在耳夹在,且只能加载一次

存储于一块固定的内存区域(静态区),可以直接被类名调用。

优先于对象存在,可以被所有对象共享

静态代码块

定义在成员位置,使用static修饰的代码块。

位置:类中方法外

执行:随着类的加载而执行且执行一次,优先main方法和构造方法的执行

作用:给类变量赋值

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
public class Game{
public static int number;
public static ArrayList<String> list;
static{
//给类变量赋值
number = 2;
list = new ArrayList<String>();
//添加元素到集合中
list.add("张三");
list.add("李四");
}
}

Static练习

基础班新班开班,学员报到。现在想为每一位新来报到的同学编学号(sid),从第一名同学开始,sid为1,以此类推。学号必须是唯一的,连续的,并且与班级的人数相符,这样以便知道,要分配给下一名新同学的学号是多少。这样我们就需要一个变量numberOfStudent,与单独的每一个学生对象无关,而是与整个班级同学数量有关。

代码

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
/**
基础班新班开班,学员报到。现在想为每一位新来报到的同学编学号(sid),从第一名同学开始,sid为1,以此类推。
学号必须是唯一的,连续的,并且与班级的人数相符,这样以便知道,要分配给下一名新同学的学号是多少。
**/

public class StaticTest01 {
private String name;
private int age;
private int sid;

public static int numberStudent = 0;

public StaticTest01(String name, int age) {
this.name = name;
this.age = age;

this.sid = ++numberStudent;

}

public void show() {
System.out.println("Student:name=" + name + ";age=" + age + ";sid=" + sid);
}
}





public class StuDemo {
public static void main(String[] args) {
StaticTest01 s1 = new StaticTest01("小老虎",10);
StaticTest01 s2 = new StaticTest01("大老虎",15);
StaticTest01 s3 = new StaticTest01("母老虎",20);
StaticTest01 s4 = new StaticTest01("公老虎",20);


s1.show();
s2.show();
s3.show();
s4.show();
}
}

Array类

概述

该类包含用来操作数组的各种方法,比如排序和搜索等。所有方法都是静态方法,所以调用简单。

操作方法

public static String toString(int[] a) : 返回指定数组内容的字符串表示形式

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Arrays;

public class ArrayDemo1 {
public static void main(String[] args) {
int[] i = {21,23,25,31};
for(int x=0;x<i.length;x++){
//遍历输出数组内容
System.out.println(i[x]);
}
String s = Arrays.toString(i);
System.out.println(s);
}
}

public static void sort(int[] a): 对指定的int型数组升序排列

1
2
3
4
5
6
7
8
9
10

import java.util.Arrays;

public class ArrayDemo1 {
public static void main(String[] args) {
int[] i = {21,23,35,31};
Arrays.sort(i);
System.out.println(Arrays.toString(i));
}
}

练习

请使用Arrays相关的API,将一个随机字符串中的所有字符升序排列,并倒序打印。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Arrays;
public class ArraysTest {
public static void main(String[] args) {
String str1 = "helloword";
char[] chars = str1.toCharArray();
for(int x=0;x<chars.length;x++) {
System.out.println(chars[x]);
}
Arrays.sort(chars);
System.out.println(Arrays.toString(chars));
for(int i=chars.length-1;i>=0;i--){
System.out.println(chars[i]+" ");
}
}
}

Math类

概述

执行基本数学运算方法,所有方法也都为静态方法

基本运算方法

public static double abs(double a) 返回Double值的绝对值。

public static double ceil(double a) 返回大于等于参数的最小的整数。

public static double floor(double a) 返回小于等于参数最大的整数。

public static long round(double a) 返回最接近参数的long。(相当于四舍五入方法)

练习

请使用Math相关的API,计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MathTest {
public static void main(String[] args) {
double min = -10.8;
double mac = 5.9;
int count = 0;

for(double i=Math.ceil(min);i<=mac;i++){
if(Math.abs(i)>6||Math.abs(i)<2.1){
count++;
}
}
System.out.println(count);
}
}