Java日常刷题基础小结(1)

本文深入讲解了Java中的字符串操作,包括charAt()方法的使用,字符串与整数的转换,以及如何利用StringBuffer和StringBuilder进行字符串的高效修改。同时,文章介绍了HashMap的使用技巧,如键值对的增删查改,以及Scanner类的next()和nextLine()方法在用户输入处理中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  1. int i = Integer.parseInt(str);
  2. 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

  1. String str = String.valueOf(i);
  2. String str = Integer.toString(i);
  3. 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

    可能有两种情况:

    1. 一种是在集合中没有该键对象
    2. 另一种是该键对象没有映射任何值对象,即值对象为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();
    }
}
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值