您的位置:首页 > 编程语言 > Java开发

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

2015-05-23 21:55 507 查看
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获取数据出现的小问题及解决方案

例:

//先获取一个int值,再获取一个字符串
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
String s = sc.nextLine();
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 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本

例:

//空构造
String s1 = new String();
System.out.println("s1"+s1);//s1
//将字节数组转成字符串
byte[] bys = {97,98,99,100,101};
String s2 = new String(bys);
System.out.println("s2:"+s2);//s2:abcde
//输出字符串的长度
System.out.println(s2.length());//5
//将字节数组的一部分转成字符串
String s3 = new String(bys,1,3);
System.out.println("s3:"+s3);//s3:bcd
//将字符数组转成字符串
char[] ch = {'a','b','c','d','e','我'};
String s4 = new String(ch);
System.out.println("s4:"+s4);//s4:abcde我
//将字符数组的一部分转成字符串
String s5 = new String(ch,2,4);
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:

public class Practice
{
public static void main(String[] args)
{
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true

String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4);//false
System.out.println(s3.equals(s4));//true

String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);//true
System.out.println(s5.equals(s6));//true
}
}


题2:

public class Practice
{
public static void main(String[] args)
{
//字符串如果是变量相加,先开空间,再拼接。
//字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);//false
System.out.println(s3.equals(s1 + s2));//true
System.out.println(s3 == "hello" + "world");//true
System.out.println(s3.equals("hello" + "world"));//true
}
}


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

例:

public class Practice
{
public static void main(String[] args)
{
//创建字符串对象
String s1 = "helloworld";
String s2 = "Helloworld";
System.out.println("equals:"+s1.equals(s2));
System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s2));
System.out.println("contains:"+s1.contains("hello"));
System.out.println("startsWith:"+s1.startsWith("he"));
System.out.println("isEmpty:"+s1.isEmpty());
}
}


运行结果:

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


12.10 模拟用户登录案例

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

分析:

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

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

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

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

import java.util.Scanner;
public class Practice
{
public static void main(String[] args)
{
//定义用户名和密码
String username = "admin";
String passworld = "12345";
//键盘录入用户名和密码
Scanner sc = new Scanner(System.in);

for (int i = 0; i < 3; i++)
{
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入密码:");
String pwd = sc.nextLine();
if(name.equals(username) && pwd.equals(passworld))
{
System.out.println("登录成功");
break;
}
else
{
if(2 - i == 0)
{
System.out.println("账号被冻结");
}
else
{
System.out.println("用户名或密码错误,还有"+(2 - i)+"次机会");
}
}
}
}
}


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

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

import java.util.Scanner;
public class Practice
{
public static void main(String[] args)
{
//定义用户名和密码
String username = "admin";
String passworld = "12345";
//键盘录入用户名和密码
Scanner sc = new Scanner(System.in);

for (int i = 0; i < 3; i++)
{
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入密码:");
String pwd = sc.nextLine();
if(name.equals(username) && pwd.equals(passworld))
{
System.out.println("登录成功,开始游戏");
GuessNumberGame.start();
break;
}
else
{
if(2 - i == 0)
{
System.out.println("账号被冻结");
}
else
{
System.out.println("用户名或密码错误,还有"+(2 - i)+"次机会");
}
}
}
}
}


import java.util.Scanner;

public class GuessNumberGame
{
private GuessNumberGame(){}
public static void start()
{
int num = (int)(Math.random()*100)+1;
//记录猜的次数
int count = 1;
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数:");
int guess = sc.nextInt();
while(guess != num)
{
if(guess > 100 || guess < 1)
{
System.out.println("数据只能在1~100之间,请重新输入:");
guess = sc.nextInt();
}
else if(guess > num)
{
System.out.print("你猜的数"+guess+"大了,");
count++;
System.out.println("进行第"+count+"次猜数:");
guess = sc.nextInt();
}
else
{
System.out.print("你猜的数"+guess+"小了,");
count++;
System.out.println("进行第"+count+"次猜数:");
guess = sc.nextInt();
}
}
System.out.println("恭喜你,猜对了,用了"+count+"次机会");
}
}


运行结果:

请输入用户名:
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):

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

例:

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


运行结果:

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


12.15 字符串的遍历

public class Practice
{
public static void main(String[] args)
{
String s = "helloworld";
for (int i = 0; i < s.length(); i++)
{
System.out.print(s.charAt(i)+" ");
}
}
}


运行结果:

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:输出结果

public class Practice
{
public static void main(String[] args)
{
//定义一个字符串
String s = "Hello123World";

//定义三个统计变量
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;

//遍历字符串,得到每一个字符。
for(int x = 0; x<s.length(); x++)
{
char ch = s.charAt(x);

//判断该字符到底是属于那种类型的
if(ch >= 'a' && ch <= 'z')
{
smallCount++;
}
else if(ch >= 'A' && ch <= 'Z')
{
bigCount++;
}
else if(ch >= '0' && ch <= '9')
{
numberCount++;
}
}
//输出结果
System.out.println("大写字母"+bigCount+"个");
System.out.println("小写字母"+smallCount+"个");
System.out.println("数字"+numberCount+"个");
}
}


运行结果:

大写字母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):

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

例:

//定义一个字符串
String s = "JavaSE";
byte[] bys = s.getBytes();
System.out.print("getBytes:");
for (int i = 0; i < bys.length; i++)
{
System.out.print(bys[i]+" ");
}
System.out.println();
char[] chs = s.toCharArray();
System.out.print("toCharArray:");
for (int i = 0; i < chs.length; i++)
{
System.out.print(chs[i]+" ");
}
System.out.println();
String ss = String.valueOf(chs);
System.out.println("valueOf:"+ss);


运行结果:

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


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

例:helloWORLD 结果:Helloworld

public class Practice
{
public static void main(String[] args)
{
//定义一个字符串
String s = "helloWORLD";
String s1 = s.substring(0, 1);
String s2 = s.substring(1);
String ss = s1.toUpperCase().concat(s2.toLowerCase());
System.out.println(ss);

//简化
String str = s.substring(0, 1).toUpperCase()
.concat(s.substring(1).toLowerCase());
System.out.println(str);

}
}


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()

例:

String s1 = "abc";
String s2 = "hijk";
String s3 = "xyz";
String s4 = "hello";
String s5 = "hel";

System.out.println(s1.compareTo(s2));//-7
System.out.println(s3.compareTo(s1));//23
//返回的是这两个字符串长度的差
System.out.println(s4.compareTo(s5));//2


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

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

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

String s = "[";
for (int i = 0; i < arr.length; i++)
{
if(i == arr.length-1)
{
s += arr[i];
s += "]";
}
else
{
s += arr[i];
s += ", ";
}
}
return s;


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

public class Practice
{
public static void main(String[] args)
{
int [] arr = {12,43,11};
String s = getString(arr);
System.out.println(s);
}
public static String getString(int[] arr)
{
String s = "[";
for (int i = 0; i < arr.length; i++)
{
if(i == arr.length-1)
{
s += arr[i];
s += "]";
}
else
{
s += arr[i];
s += ", ";
}
}
return s;
}
}


12.24 字符串反转的案例

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

import java.util.Scanner;

public class Practice
{
public static void main(String[] args)
{
//定义一个空的字符串
String s = " ";
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串:");
String line = sc.nextLine();
char[] ch = line.toCharArray();
for (int i = ch.length - 1; i >= 0; i--)
{
s += ch[i];
}
System.out.println(s);
}
}


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

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

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





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

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

public class Practice
{
public static void main(String[] args)
{
String s = "woaijavawozhenaijavawozhendeaijavaw" +
"ozhendehenaijavaxinbuxinwoaijavagun";
String key = "java";
int count = 0;
int index;
while((index = s.indexOf(key)) != -1)
{
count++;
s = s.substring(index+key.length());
}
System.out.println(count);
}
}


12.28 断点查看在大串中查找小串出现的次数案例
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: