学习笔记

xiaoxiao2021-02-28  49

1:Scanner的使用(了解) (1)在JDK5以后出现的,用于键盘录入数据的类。 (2)构造方法: A:讲解了System.in这个东西。 它其实是标准的输入流,对应于键盘录入 B:构造方法 InputStream is = System.in; Scanner(InputStream is) C:常用的格式 Scanner sc = new Scanner(System.in); (3)基本方法格式: A:hasNextXxx() 判断是否是某种类型的 B:nextXxx() 返回某种类型的元素 (4)要掌握的两个方法 A:public int nextInt() int类型 B:public String nextLine() String类型 (5)需要注意的小问题 A:同一个Scanner对象,先获取数值,再获取字符串,会出现一个小问题。 B:解决方案: a:重新定义一个Scanner对象 b:把所有的数据,都用字符串获取,然后再进行相应的转换。 2:String类的概述和使用(掌握) (1)多个字符组成的一串数据。 其实,它可以和字符数组进行相互转换。 (2)构造方法: A:public String() 空构造 B:public String(byte[] bytes) 把字节数组,转成字符串 C:public String(byte[] bytes,int offset,int length) 把字节数组的一部分(从某个索引开始的几个),转成字符串 D:public String(char[] value) 把字符数组,转成字符串 E:public String(char[] value,int offset,int count) 把字符数组的一部分,转成字符串 F:public String(String original) 把字符串常量值,转成字符串 下面的这一个虽然不是构造方法,但是,结果也是一个字符串对象 G:String s = "hello";直接赋值 (3)字符串的特点 A:字符串,一旦被赋值,就不能改变。 注意:这里指的是字符串的内容不能改变,而不是引用不能改变。 B:字面值作为字符串对象 和 通过构造方法创建对象 的不同 String s = new String("hello"); 和 String s = "hello" 的区别? 一个是在方法区和堆里都有,一个是在方法区有 (4)字符串的面试题(看程序写结果) A:==和equals() 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 B:字符串的拼接 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"); // false 这个,我们错了,应该是true System.out.println(s3.equals("hello" + "world")); // true (5)字符串的功能 (自己补齐方法的中文意思) A:判断功能 boolean equals(Object obj) 比较字符串的内容是否相同,区分大小写         重写父类的方法equals 所以是Object类型 boolean equalsIgnoreCase(String str) 比较字符串的内容是否相同,忽略大小写 验证码不区分大小写 boolean contains(String str) 判断大字符串中是否包含小字符串 boolean startsWith(String str)  判断字符串是否以某个指定的字符串 开头 boolean endsWith(String str) 判断字符串是否以某个指定的字符串 结尾 boolean isEmpty() 判断字符串是否为空。 注意:字符串内容为空和字符串对象为空。 String s = "";    字符串内容为空   对象存在,但没有数据 String s = null;  字符串对象为空 对象不存在 B:获取功能 int length() 获取字符串的长度。 char charAt(int index) 获取指定索引位置的字符 int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。 为什么这里是int类型,而不是char类型? 原因是:'a'和97其实都可以代表'a'  int类型是大的类型 int indexOf(String str) 返回指定字符串在此字符串中第一次出现处的索引。 int indexOf(int ch,int fromIndex) 返回指定字符在此字符串中从指定位置后第一次出现处的索引。 int indexOf(String str,int fromIndex) 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。 String substring(int start) 从指定位置开始截取字符串,默认到末尾。 String substring(int start,int end) 从指定位置开始到指定位置结束,截取字符串。 C:转换功能 byte[] getBytes() 把字符串转换为,字节数组。 char[] toCharArray() 把字符串转换为,字符数组。 遍历字符串有两种方法:前面讲的charAt()和length()方法结合;   toCharArray()方法,把字符串转换为,字符数组 static String valueOf(char[] chs)   把字符数组转成,字符串。 static String valueOf(int i) 把int类型的数据转成,字符串。 注意:String类的valueOf方法,可以把任意类型的数据转成字符串。   API里叫方法重载(overload) String toLowerCase() 把字符串转成,小写。 String toUpperCase() 把字符串转成,大写。 String concat(String str) 把字符串,拼接。 D:其他功能 a:替换功能  String replace(char old,char new) String replace(String old,String new) b:去(两端)空格功能 String trim() 去除字符串两空格 去除前后两端的,不去除中间的 c:按字典比较功能 int compareTo(String str) 按字典顺序,比较两个字符串  区分大小写的比较 int compareToIgnoreCase(String str) 按字典顺序,比较两个字符串  不区分大小写的比较 (6)字符串的案例 A:模拟用户登录 B:字符串遍历 C:统计字符串中大写,小写,及数字字符的个数 D:把字符串的首字母转成大写,其他小写 E:把int数组拼接成一个指定格式的字符串 F:字符串反转 G:统计大串中小串出现的次数
转载请注明原文地址: https://www.6miu.com/read-2620766.html

最新回复(0)