JavaSE学习总结第12天_API常用对象2

12.01 Scanner的概述和构造方法原理

Scanner类概述:JDK5以后用于获取用户的键盘输入

构造方法:public Scanner(InputStream source)

public static final InputStream in:“标准”输入流。

此流已打开并准备提供输入数据。通常,此流对应于键盘输入或者由主机环境或用户指定的另一个输入源。

12.02 Scanner类的hasNextXxx()和nextXxx()方法的讲解

基本格式:

hasNextXxx():判断是否还有下一个输入项,其中Xxx可以是Int,Double等。

 如果需要判断是否包含下一个字符串,则可以省略Xxx

nextXxx():获取下一个输入项。Xxx的含义和上个方法中的Xxx相同

默认情况下,Scanner使用空格,回车等作为分隔符

 

常用方法:

public int nextInt(int radix):将输入信息的下一个标记扫描为一个int

public String nextLine():此扫描器执行当前行,并返回跳过的输入信息。此方法返回当前行的其余部分,不包括结尾处的行分隔符。

12.03 Scanner获取数据出现的小问题及解决方案

例:

1 //先获取一个int值,再获取一个字符串
2 Scanner sc = new Scanner(System.in);
3 int x = sc.nextInt();
4 String s = sc.nextLine();
5 System.out.println("x:"+x+"  s:"+s);

运行结果:

10
x:10  s:

上例中先获取一个int值,再获取一个字符串时,字符串的值是空的,因为Scanner使用空格回车等作为分隔符,当回车换行时将换行符号赋给了s

解决方案:

1:重新创建对象

2:都以字符串形式接收,然后把字符串转成int类型

12.04 String类的概述

String类概述:字符串是由多个字符组成的一串数据(字符序列),字符串可以看成是字符数组,字符串是常量;它们的值在创建之后不能更改。

12.05 String类的构造方法

构造方法:

1.  public String():

初始化一个新创建的 String 对象,使其表示一个空字符序列。注意,由于 String 是不可变的,所以无需使用此构造方法

2.  public String(byte[] bytes):

通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String

3.  public String(byte[] bytes,int offset,int length):

通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String

4.  public String(char[] value):

分配一个新的 String,使其表示字符数组参数中当前包含的字符序列

5.  public String(char[] value,int offset,int count):

分配一个新的 String,它包含取自字符数组参数一个子数组的字符

6.  public String(String original):

初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本

例:

复制代码
 1 //空构造
 2 String s1 = new String();
 3 System.out.println("s1"+s1);//s1
 4 //将字节数组转成字符串
 5 byte[] bys = {97,98,99,100,101};
 6 String s2 = new String(bys);
 7 System.out.println("s2:"+s2);//s2:abcde
 8 //输出字符串的长度
 9 System.out.println(s2.length());//5
10 //将字节数组的一部分转成字符串
11 String s3 = new String(bys,1,3);
12 System.out.println("s3:"+s3);//s3:bcd
13 //将字符数组转成字符串
14 char[] ch = {'a','b','c','d','e','我'};
15 String s4 = new String(ch);
16 System.out.println("s4:"+s4);//s4:abcde我
17 //将字符数组的一部分转成字符串
18 String s5 = new String(ch,2,4);
19 System.out.println("s5:"+s5);//s5:cde我
复制代码

12.06 String的特点

String的特点:一旦被赋值就不能改变

String s = “hello”; s += “world”; s的结果是多少?

String s = “hello”; 在方法区的字符串常量池中创建hello

s += “world”;在方法区的字符串常量池中创建world ,再创建空间拼接helloworld

s的结果为helloworld

12.07 String字面值对象和构造方法创建对象的区别

String s = new String(“hello”) 和 String s = “hello”;的区别?

有区别,String s = new String(“hello”);分别在堆内存与方法区创建2个对象

  String s = “hello”;只在方法区创建1个对象

12.08 String面试题看程序写结果

题1:

复制代码
 1 public class Practice 
 2 {
 3     public static void main(String[] args)
 4     {
 5         String s1 = new String("hello");
 6         String s2 = new String("hello");
 7         System.out.println(s1==s2);//false
 8         System.out.println(s1.equals(s2));//true
 9 
10         String s3 = new String("hello");
11         String s4 = "hello";
12         System.out.println(s3==s4);//false
13         System.out.println(s3.equals(s4));//true
14                 
15         String s5 = "hello";
16         String s6 = "hello";
17         System.out.println(s5==s6);//true
18         System.out.println(s5.equals(s6));//true
19     }
20 }
复制代码

题2:

复制代码
 1 public class Practice 
 2 {
 3     public static void main(String[] args)
 4     {
 5         //字符串如果是变量相加,先开空间,再拼接。
 6         //字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
 7         String s1 = "hello";
 8         String s2 = "world";
 9         String s3 = "helloworld";
10         System.out.println(s3 == s1 + s2);//false
11         System.out.println(s3.equals(s1 + s2));//true
12         System.out.println(s3 == "hello" + "world");//true
13         System.out.println(s3.equals("hello" + "world"));//true
14     }
15 }
复制代码

12.09 String类的判断功能

1.  public boolean equals(Object anObject):

将此字符串与指定的对象比较

2.  public boolean equalsIgnoreCase(String anotherString):

将此 String 与另一个 String 比较,不考虑大小写

3.  public boolean contains(CharSequence s):

当且仅当此字符串包含指定的 char 值序列时,返回 true

4.  public boolean startsWith(String prefix):

测试此字符串是否以指定的前缀开始

5.  public boolean endsWith(String suffix):

测试此字符串是否以指定的后缀结束

6.  public boolean isEmpty():

当且仅当 length()为0时返回 true

例:

复制代码
 1 public class Practice 
 2 {
 3     public static void main(String[] args)
 4     {
 5         //创建字符串对象
 6         String s1 = "helloworld";
 7         String s2 = "Helloworld";
 8         System.out.println("equals:"+s1.equals(s2));
 9         System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s2));
10         System.out.println("contains:"+s1.contains("hello"));
11         System.out.println("startsWith:"+s1.startsWith("he"));
12         System.out.println("isEmpty:"+s1.isEmpty());
13     }
14 }
复制代码

运行结果:

equals:false
equalsIgnoreCase:true
contains:true
startsWith:true
isEmpty:false

12.10 模拟用户登录案例

模拟登录,给三次机会,并提示还有几次

分析:

1:定义用户名和密码。已存在的。

2:键盘录入用户名和密码。

3:比较用户名和密码。如果都相同,则登录成功,如果有一个不同,则登录失败。

4:给三次机会,用循环改进,用for循环。

复制代码
 1 import java.util.Scanner;
 2 public class Practice 
 3 {
 4     public static void main(String[] args)
 5     {
 6         //定义用户名和密码
 7         String username = "admin";
 8         String passworld = "12345";
 9         //键盘录入用户名和密码
10         Scanner sc = new Scanner(System.in);
11         
12         for (int i = 0; i < 3; i++) 
13         {
14             System.out.println("请输入用户名:");
15             String name = sc.nextLine();
16             System.out.println("请输入密码:");
17             String pwd = sc.nextLine();
18             if(name.equals(username) && pwd.equals(passworld))
19             {
20                 System.out.println("登录成功");
21                 break;
22             }
23             else
24             {
25                 if(2 - i == 0)
26                 {
27                     System.out.println("账号被冻结");
28                 }
29                 else
30                 {
31                     System.out.println("用户名或密码错误,还有"+(2 - i)+"次机会");
32                 }
33             }
34         }
35     }
36 }
复制代码

12.11 断点查看模拟用户登录案例

12.12 模拟用户登录案例增强版加入猜数字游戏

复制代码
 1 import java.util.Scanner;
 2 public class Practice 
 3 {
 4     public static void main(String[] args)
 5     {
 6         //定义用户名和密码
 7         String username = "admin";
 8         String passworld = "12345";
 9         //键盘录入用户名和密码
10         Scanner sc = new Scanner(System.in);
11         
12         for (int i = 0; i < 3; i++) 
13         {
14             System.out.println("请输入用户名:");
15             String name = sc.nextLine();
16             System.out.println("请输入密码:");
17             String pwd = sc.nextLine();
18             if(name.equals(username) && pwd.equals(passworld))
19             {
20                 System.out.println("登录成功,开始游戏");
21                 GuessNumberGame.start();
22                 break;
23             }
24             else
25             {
26                 if(2 - i == 0)
27                 {
28                     System.out.println("账号被冻结");
29                 }
30                 else
31                 {
32                     System.out.println("用户名或密码错误,还有"+(2 - i)+"次机会");
33                 }
34             }
35         }
36     }
37 }
复制代码
复制代码
 1 import java.util.Scanner;
 2 
 3 public class GuessNumberGame 
 4 {
 5     private GuessNumberGame(){}
 6     public static void start()
 7     {
 8         int num = (int)(Math.random()*100)+1;
 9         //记录猜的次数
10         int count = 1;
11         Scanner sc = new Scanner(System.in);
12         System.out.println("请输入一个数:");
13         int guess = sc.nextInt();
14         while(guess != num)
15         {
16             if(guess > 100 || guess < 1)
17             {
18                 System.out.println("数据只能在1~100之间,请重新输入:");
19                 guess = sc.nextInt();
20             }    
21             else if(guess > num)
22             {
23                 System.out.print("你猜的数"+guess+"大了,");
24                 count++;
25                 System.out.println("进行第"+count+"次猜数:");
26                 guess = sc.nextInt();
27             }
28             else
29             {
30                 System.out.print("你猜的数"+guess+"小了,");
31                 count++;
32                 System.out.println("进行第"+count+"次猜数:");
33                 guess = sc.nextInt();
34             }
35         }
36         System.out.println("恭喜你,猜对了,用了"+count+"次机会");
37     }
38 }
复制代码

运行结果:

复制代码
请输入用户名:
sd
请输入密码:
ger
用户名或密码错误,还有2次机会
请输入用户名:
admin
请输入密码:
12345
登录成功,开始游戏
请输入一个数:
58
你猜的数58大了,进行第2次猜数:
45
你猜的数45大了,进行第3次猜数:
23
你猜的数23大了,进行第4次猜数:
12
你猜的数12大了,进行第5次猜数:
5
恭喜你,猜对了,用了5次机会
复制代码

12.13 断点查看模拟用户登录案例增强版加入猜数字游戏

12.14 String类的获取功能

1.  public int length():

返回此字符串的长度

2.  public char charAt(int index):

返回指定索引处的 char 值。索引范围为从 0 到 length() - 1

3.  public int indexOf(int ch):

返回指定字符在此字符串中第一次出现处的索引

4.  public int indexOf(String str):

返回指定子字符串在此字符串中第一次出现处的索引

5.  public int lastIndexOf(int ch,int fromIndex):

返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索

6.  public int indexOf(String str,int fromIndex):

返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始

7.  public String substring(int beginIndex):

返回一个新的字符串,它是此字符串的一个子字符串

8.  public String substring(int beginIndex,int endIndex):

返回一个新字符串,它是此字符串的一个子字符串

例:

1 String s = "helloworld";
2 System.out.println("length:"+s.length());
3 System.out.println("charAt:"+s.charAt(4));
4 System.out.println("indexOf:"+s.indexOf('l'));
5 System.out.println("indexOf:"+s.indexOf("owo"));
6 System.out.println("substring:"+s.substring(2, 6));

运行结果:

length:10
charAt:o
indexOf:2
indexOf:4
substring:llow

12.15 字符串的遍历

复制代码
 1 public class Practice 
 2 {
 3     public static void main(String[] args)
 4     {
 5         String s = "helloworld";
 6         for (int i = 0; i < s.length(); i++) 
 7         {
 8             System.out.print(s.charAt(i)+" ");
 9         }
10     }
11 }
复制代码

运行结果:

h e l l o w o r l d 

12.16 统计大写,小写及数字字符的个数案例

统计一个字符串中大写字母、小写字母、数字出现的次数

例:Hello123World 大写2个小写8个数字3个

分析:

前提:字符串要存在

1:定义三个统计变量

bigCount=0

smallCount=0

numberCount=0

2:遍历字符串,得到每一个字符。length()和charAt()结合

3:判断该字符到底是属于那种类型的

这道题目的难点就是如何判断某个字符是大的,还是小的,还是数字的。

char ch = s.charAt(x);

if(ch>='0' && ch<='9') numberCount++

if(ch>='a' && ch<='z') smallCount++

if(ch>='A' && ch<='Z') bigCount++

4:输出结果

复制代码
 1 public class Practice 
 2 {
 3     public static void main(String[] args)
 4     {
 5         //定义一个字符串
 6         String s = "Hello123World";
 7                 
 8         //定义三个统计变量
 9         int bigCount = 0;
10         int smallCount = 0;
11         int numberCount = 0;
12         
13         //遍历字符串,得到每一个字符。
14         for(int x = 0; x<s.length(); x++)
15         {
16             char ch = s.charAt(x);
17             
18             //判断该字符到底是属于那种类型的
19             if(ch >= 'a' && ch <= 'z')
20             {
21                 smallCount++;
22             }
23             else if(ch >= 'A' && ch <= 'Z')
24             {
25                 bigCount++;
26             }
27             else if(ch >= '0' && ch <= '9')
28             {
29                 numberCount++;
30             }
31         }
32         //输出结果
33         System.out.println("大写字母"+bigCount+"个");
34         System.out.println("小写字母"+smallCount+"个");
35         System.out.println("数字"+numberCount+"个");
36     }
37 }
复制代码

运行结果:

大写字母2个
小写字母8个
数字3个

12.17 断点查看统计大写,小写及数字字符的个数案例

12.18 String类的转换功能

1.  public byte[] getBytes():

使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中

2.  public char[] toCharArray():

将此字符串转换为一个新的字符数组

3.  public static String valueOf(char[] data):

返回 char 数组参数的字符串表示形式

4.  public static String valueOf(long l):

返回 long 参数的字符串表示形式

5.  public String toLowerCase():

使用默认语言环境的规则将此 String 中的所有字符都转换为小写

6.  public String toUpperCase():

使用默认语言环境的规则将此 String 中的所有字符都转换为大写

7.  public String concat(String str):

将指定字符串连接到此字符串的结尾

例:

复制代码
 1 //定义一个字符串
 2 String s = "JavaSE";
 3 byte[] bys = s.getBytes();
 4 System.out.print("getBytes:");
 5 for (int i = 0; i < bys.length; i++) 
 6 {
 7     System.out.print(bys[i]+" ");
 8 }
 9 System.out.println();
10 char[] chs = s.toCharArray();
11 System.out.print("toCharArray:");
12 for (int i = 0; i < chs.length; i++) 
13 {
14     System.out.print(chs[i]+" ");
15 }
16 System.out.println();
17 String ss = String.valueOf(chs);
18 System.out.println("valueOf:"+ss);
复制代码

运行结果:

getBytes:74 97 118 97 83 69 
toCharArray:J a v a S E 
JavaSE

12.19 把字符串的首字母转大写其他转小写

例:helloWORLD 结果:Helloworld

复制代码
 1 public class Practice 
 2 {
 3     public static void main(String[] args)
 4     {
 5         //定义一个字符串
 6         String s = "helloWORLD";
 7         String s1 = s.substring(0, 1);
 8         String s2 = s.substring(1);
 9         String ss = s1.toUpperCase().concat(s2.toLowerCase());
10         System.out.println(ss);
11         
12         //简化
13         String str = s.substring(0, 1).toUpperCase()
14                 .concat(s.substring(1).toLowerCase());
15         System.out.println(str);
16         
17     }
18 }
复制代码

12.20 String类的其他功能

1.  public String replace(char oldChar,char newChar):

返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的

2.  public String replace(String old,String new)

3.  public String trim():返回字符串的副本,忽略前导空白和尾部空白

4.  public int compareTo(String anotherString):按字典顺序比较两个字符串

5.  public int compareToIgnoreCase(String str):按字典顺序比较两个字符串,不考虑大小写

如果没有字符不同的索引位置,则较短字符串的字典顺序在较长字符串之前。在这种情况下,compareTo 返回这两个字符串长度的差,即值:this.length()-anotherString.length()

例:

复制代码
 1 String s1 = "abc";
 2 String s2 = "hijk";
 3 String s3 = "xyz";
 4 String s4 = "hello";
 5 String s5 = "hel";
 6 
 7 System.out.println(s1.compareTo(s2));//-7
 8 System.out.println(s3.compareTo(s1));//23
 9 //返回的是这两个字符串长度的差
10 System.out.println(s4.compareTo(s5));//2
复制代码

12.21 String类的compareTo()方法的源码解析

12.22 把int数组拼接字符串的案例

把数组中的数据按照指定个格式拼接成一个字符串例:int[] arr = {1,2,3}; 输出结果:[1, 2, 3]

复制代码
 1 String s = "[";
 2 for (int i = 0; i < arr.length; i++) 
 3 {
 4     if(i == arr.length-1)
 5     {
 6         s += arr[i];
 7         s += "]";
 8     }
 9     else
10     {
11         s += arr[i];
12         s += ", ";
13     }
14 }
15 return s;
复制代码

12.23 把int数组拼接成字符串的案例改进版

复制代码
 1 public class Practice 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         int [] arr = {12,43,11};
 6         String s = getString(arr);
 7         System.out.println(s);
 8     }
 9     public static String getString(int[] arr)
10     {
11         String s = "[";
12         for (int i = 0; i < arr.length; i++) 
13         {
14             if(i == arr.length-1)
15             {
16                 s += arr[i];
17                 s += "]";
18             }
19             else
20             {
21                 s += arr[i];
22                 s += ", ";
23             }
24         }
25         return s;
26     }
27 }
复制代码

12.24 字符串反转的案例

字符串反转例:键盘录入"abc" 输出结果:"cba"

复制代码
 1 import java.util.Scanner;
 2 
 3 public class Practice 
 4 {
 5     public static void main(String[] args) 
 6     {
 7         //定义一个空的字符串
 8         String s = " ";
 9         Scanner sc = new Scanner(System.in);
10         System.out.println("请输入字符串:");
11         String line = sc.nextLine();
12         char[] ch = line.toCharArray();
13         for (int i = ch.length - 1; i >= 0; i--) 
14         {
15             s += ch[i];
16         }
17         System.out.println(s);
18     }
19 }
复制代码

12.25 在大串中查找小串出现的次数案例思路图解

统计大串中小串出现的次数

例:在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"中java出现了5次

 

12.26 在大串中查找小串出现的次数案例代码实现

12.27 在大串中查找小串出现的次数案例代码优化

复制代码
 1 public class Practice 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         String s = "woaijavawozhenaijavawozhendeaijavaw" +
 6                 "ozhendehenaijavaxinbuxinwoaijavagun";
 7         String key = "java";
 8         int count = 0;
 9         int index;
10         while((index = s.indexOf(key)) != -1)
11         {
12             count++;
13             s = s.substring(index+key.length());
14         }
15         System.out.println(count);
16     }
17 }
原文地址:https://www.cnblogs.com/hoop-superman/p/5495701.html