charAt(int index) 方法
java.lang.String.charAt();
public char charAt(int index);
- index:字符的索引值。 索引范围是从 0 到 length() - 1。
- 返回值:字符串中索引index所指的char值。
String A = "abcdef";
char a = A.charAt(0);
// a = 'a';
System.out.println(a);
String 和 int 互相转换(Number)
String ==> int
- int i = Integer.parseInt(str);
- int i = Integer.valueOf(str).intValue();
String str = "123456";
//1.
int i = Integer.parseInt(str);
//将字符串解析为int类型
//2.
int i = Integer.valueOf(str).intValue();
//所有的包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类 Number 的子类。
//valueOf() 方法用于返回给定参数的原生 Number 对象值,参数可以是原生数据类型, String等
//intValue() :以 int 形式返回指定的数值。
//doubleValue() :以 double 形式返回指定的数值。
int ==> String
- String str = String.valueOf(i);
- String str = Integer.toString(i);
- String str = i + “”;
int i = 100;
//1.
String str = String.valueOf(i);
//2.
String str = Integer.toString(i);
//toString() 方法用于返回以一个字符串表示的 Number 对象值。Number.toString(x)
//3.
String str = i + "";
//字符串和基本数据类型相加,结果为字符串
StringBuffer 和 StringBuilder 类
当对字符串进行修改
的时候,需要使用 StringBuffer 和 StringBuilder 类。
StringBuilder 和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。由于 StringBuilder
相较于 StringBuffer 有速度优势
,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全
的情况下,则必须使用 StringBuffer
类。
StringBuffer类
public StringBuffer append(String s):将指定的字符串追加到此字符序列。
public StringBuffer reverse():将此字符序列用其反转形式取代。
public insert(int offset, int i):将 i 的字符串表示形式插入此序列下标offset处。
public StringBuffer delete(a, b):移除此序列[a, b)的子字符串中的字符。
StringBuffer strB = new StringBuffer("123"); //123
strB.append("456"); //123456
strB.reverse(); //654321
strB.reverse(); //123456
strB.insert(0, "Hello"); //Hello123456
strB.delete(0,2); //llo123456 删除下标 0, 1 对应字符
System.out.println(strB); //llo123456
HashMap
HashMap 是一个散列表,它存储的内容是键值对(key-value)
映射。
HashMap 实现了 Map 接口,根据键的 HashCode 值存储数据,具有很快的访问速度,最多允许一条记录的键为 null。
添加
键值对(key-value)可以使用put(key, value)
方法- 使用
get(key)
方法来获取
key 对应的 value - 使用
remove(key)
方法来删除 key 对应的键值对
(key-value) 删除所有键值对
(key-value)可以使用clear()
方法
Map<Integer, String> Sites = new HashMap<Integer, String>();
// or HashMap<Integer, String> Sites = new HashMap<Integer, String>();
Sites.put(1, "Google");
Sites.put(2, "Tencent");
Sites.put(3, "Baidu");
System.out.println(Sites); //{1=Google, 2=Tencent, 3=Baidu}
System.out.println(Sites.get(2)); //Tencent
Sites.remove(2); //{1=Google, 3=Baidu}
Sites.clear(); //{}
判断是否存在key使用containskey(key)
方法
-
boolean contains = map.containsKey(key);
在集合中
没有该键
对象,即没有该键值对 -
get(key) == null
可能有两种情况:
- 一种是在集合中
没有该键对象
- 另一种是该键对象没有映射任何值对象,即
值对象为null
- 一种是在集合中
Scanner 类
Scanner 类来获取用户的输入。
创建 Scanner 对象的基本语法:
Scanner s = new Scanner(System.in);
Scanner 类的next() 与 nextLine()
方法获取输入的字符串,在读取前我们一般需要 使用 hasNext() 与 hasNextLine()
判断是否还有输入
的数据。
使用 next() 方法
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
// 从键盘接收数据
Scanner scan = new Scanner(System.in);
// next方式接收字符串
System.out.println("next方式接收:");
// 判断是否还有输入
if (scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
$ java ScannerDemo
next方式接收:
Hello World
输入的数据为:Hello
可以看到 World 字符串并未输出。next() 不能得到带有空格的字符串。
使用 nextLine() 方法
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判断是否还有输入
if (scan.hasNextLine()) {
String str2 = scan.nextLine();
System.out.println("输入的数据为:" + str2);
}
scan.close();
}
}
$ java ScannerDemo
next方式接收:
Hello World
输入的数据为:Hello World
可以看到 World 字符串输出。
next() 与 nextLine() 区别
next():
- 一定要
读取到有效字符
后才可以结束输入
。 - 对
输入有效字符之前
遇到的空白
,next() 方法会自动将其去掉
。 - 只有
输入有效字符后
才将其后面输入的空白
作为分隔符或者结束符
。 - next() 不能得到带有空格的字符串。
nextLine():
- 以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
可以获得空白
。
如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx()
方法进行验证,再使用 nextXxx()
来读取。
验证:scanner.hasNextInt
()
读取:scanner.nextInt
()
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
int i = 0;
float f = 0.0f;
System.out.print("输入整数:");
if (scan.hasNextInt()) {
// 判断输入的是否是整数
i = scan.nextInt();
// 接收整数
System.out.println("整数数据:" + i);
} else {
// 输入错误的信息
System.out.println("输入的不是整数!");
}
scan.close();
}
}