一、本单元知识点概述

(Ⅰ)知识点概述

二、本单元教学目标

(Ⅰ)重点知识目标

1.ArrayList集合的常用方法
2.ArrayList存储数据和遍历数据
3.String类的构造方法
4.String类的成员方法

(Ⅱ)能力目标

1.能够使用数组存储自定义类型并遍历
2.掌握使用ArrayList集合的构造方法创建ArrayList集合对象
3.掌握使用ArrayList集合存储数据
4.掌握使用ArrayList集合中常用的方法
5.掌握使用ArrayList集合存储字符串并遍历
6.掌握使用ArrayList集合存储自定义对象并遍历
7.掌握使用ArrayList类作为形式参数和返回值类型
8.掌握使用String类的构造方法创建字符串对象
9.掌握明确String类的构造方法创建对象,和直接赋值创建字符串对象的区别
10.掌握使用文档查询String类的判断方法
11.掌握使用文档查询String类的获取方法
12.掌握使用文档查询String类的转换方法

三、本单元知识详讲

11.1 ArrayList类

11.1.1 引入——对象数组 ★★★★★

使用学生数组,存储三个学生对象,代码如下:

 1 public class Student {
2 private String name;
3 private int age;
4 public Student() { }
5 public Student(String name, int age) {
6 this.name = name; this.age = age;
7 }
8 public String getName() {
9 return name;
10 }
11 public void setName(String name) {
12 this.name = name;
13 }
14 public int getAge() {
15 return age;
16 }
17 public void setAge(int age) {
18 this.age = age;
19 }
20 }
21
22 public class Test01StudentArray {
23 public static void main(String[] args) {
24 //创建学生数组
25 Student[] students = new Student[3];
26 //创建学生对象
27 Student s1 = new Student("曹操",40);
28 Student s2 = new Student("刘备",35);
29 Student s3 = new Student("孙权",30);
30 //把学生对象作为元素赋值给学生数组
31 students[0] = s1;
32 students[1] = s2;
33 students[2] = s3;
34 //遍历学生数组
35 for(int x=0; x<students.length; x++) {
36 Student s = students[x];
37 System.out.println(s.getName()+"‐‐‐"+s.getAge());
38 }
39 }
40 }

到目前为止,我们想存储对象数据,选择的容器,只有对象数组。而数组的长度是固定的,无法适应数据变化的需

求。为了解决这个问题,Java提供了另一个容器 java.util.ArrayList 集合类,让我们可以更便捷的存储和操作对象数据。

11.1.2 什么是ArrayList类 ★★★

java.util.ArrayList 是大小可变的数组的实现,存储在内的数据称为元素。此类提供一些方法来操作内部存储的元素。 ArrayList 中可不断添加元素,其大小也自动增长。

11.1.3 ArrayList使用步骤 ★★★

  • 查看类

    java.util.ArrayList <E> :该类需要 import导入使后使用。<E> ,表示一种指定的数据类型,叫做泛型。 E ,取自Element(元素)的首字母。在出现 E 的地方,我们使用一种引用数据类型将其替换即可,表示我们将存储哪种引用类型的元素。代码如下:

ArrayList<String>,ArrayList<Student> 

查看构造方法

public ArrayList() :构造一个内容为空的集合。

基本格式: 在JDK 7后,右侧泛型的尖括号之内可以留空,但是<>仍然要写。简化格式:

ArrayList<String> list = new ArrayList<>();
  • 查看成员方法

    public boolean add(E e) : 将指定的元素添加到此集合的尾部。

    参数 E e ,在构造ArrayList对象时, <E> 指定了什么数据类型,那么 add(E e) 方法中,只能添加什么数据

    类型的对象。

    使用ArrayList类,存储三个字符串元素,代码如下:

 1 public class Test02StudentArrayList {
2 public static void main(String[] args) {
3 //创建学生数组
4 ArrayList<String> list = new ArrayList<>();
5 //创建学生对象
6 String s1 = "曹操";
7 String s2 = "刘备";
8 String s3 = "孙权";
9 //打印学生ArrayList集合
10 System.out.println(list);
11 //把学生对象作为元素添加到集合
12 list.add(s1);
13 list.add(s2);
14 list.add(s3);
15 //打印学生ArrayList集合
16 System.out.println(list);
17 }
18 }

11.1.4 常用方法和遍历★★★★★

对于元素的操作,基本体现在——增、删、查。常用的方法有:

  • public boolean add(E e) :将指定的元素添加到此集合的尾部。

  • public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。

  • public E get(int index) :返回此集合中指定位置上的元素。返回获取的元素。

  • public int size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。

这些都是最基本的方法,操作非常简单,代码如下:

 1 public class Demo01ArrayListMethod {
2 public static void main(String[] args) {
3 //创建集合对象
4 ArrayList<String> list = new ArrayList<String>();
5 //添加元素
6 list.add("hello");
7 list.add("world");
8 list.add("java");
9 //public E get(int index):返回指定索引处的元素
10 System.out.println("get:"+list.get(0));
11 System.out.println("get:"+list.get(1));
12 System.out.println("get:"+list.get(2));
13 //public int size():返回集合中的元素的个数
14 System.out.println("size:"+list.size());
15 //public E remove(int index):删除指定索引处的元素,返回被删除的元素
16 System.out.println("remove:"+list.remove(0));
17 //遍历输出 for(int i = 0; i < list.size(); i++){
18 System.out.println(list.get(i));
19 }
20 }
21 }
22

11.1.5 如何存储基本数据类型 ★★★★

ArrayList对象不能存储基本类型,只能存储引用类型的数据。但是存储基本数据类型对应的包装类型是可以的。所以,想要存储基本类型数据, <> 中的数据类型,必须转换后才能编写,转换写法如下:

基本类型 基本类型包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

我们发现,只有 Integer 和 Character 需要特殊记忆,其他基本类型只是首字母大写即可。那么存储基本类型数 据,代码如下:

 1 public class Demo02ArrayListMethod {
2 public static void main(String[] args) {
3 ArrayList<Integer> list = new ArrayList<Integer>();
4 list.add(1);
5 list.add(2);
6 list.add(3);
7 list.add(4);
8 System.out.println(list);
9 }
10 }

11.1.6 ArrayList练习 ★★★

  • 数值添加到集合

    生成6个1~33之间的随机整数,添加到集合,并遍历

 1 public class Test01ArrayList {
2 public static void main(String[] args) {
3 // 创建Random 对象
4 Random random = new Random();
5 // 创建ArrayList 对象
6 ArrayList<Integer> list = new ArrayList<>();
7 // 添加随机数到集合
8 for (int i = 0; i < 6; i++) {
9 int r = random.nextInt(33) + 1;
10 list.add(r);
11 }
12 // 遍历集合输出
13 for (int i = 0; i < list.size(); i++) {
14 System.out.println(list.get(i));
15 }
16 }
17 }

对象添加到集合

自定义4个学生对象,添加到集合,并遍历

 1 public class Test02ArrayList {
2 public static void main(String[] args) {
3 //创建集合对象
4 ArrayList<Student> list = new ArrayList<Student>();
5 //创建学生对象
6 Student s1 = new Student("赵丽颖",18);
7 Student s2 = new Student("唐嫣",20);
8 Student s3 = new Student("景甜",25);
9 Student s4 = new Student("柳岩",19);
10 //把学生对象作为元素添加到集合中
11 list.add(s1);
12 list.add(s2);
13 list.add(s3);
14 list.add(s4);
15 //遍历集合
16 for(int x = 0; x < list.size(); x++) {
17 Student s = list.get(x);
18 System.out.println(s.getName()+"‐‐‐"+s.getAge());
19 }
20 }
21 }

打印集合方法

定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起集合,使用@分隔每个元素。格式参照 {元素 @元素@元素}。

 1 public class Test03ArrayList {
2 public static void main(String[] args) {
3 // 创建集合对象
4 ArrayList<String> list = new ArrayList<String>();
5 // 添加字符串到集合中
6 list.add("张三丰");
7 list.add("宋远桥");
8 list.add("张无忌");
9 list.add("殷梨亭");
10 // 调用方法
11 printArrayList(list);
12 }
13
14 public static void printArrayList(ArrayList<String> list) {
15 // 拼接左括号
16 System.out.print("{");
17 // 遍历集合
18 for (int i = 0; i < list.size(); i++) {
19 // 获取元素
20 String s = list.get(i);
21 // 拼接@符号
22 if (i != list.size() ‐ 1) {
23 System.out.print(s + "@");
24 } else {
25 // 拼接右括号
26 System.out.print(s + "}");
27 }
28 }
29 }
30 }

获取集合方法

定义获取所有偶数元素集合的方法(ArrayList类型作为返回值)

 1 public class Test04ArrayList {
2 public static void main(String[] args) {
3 // 创建Random 对象
4 Random random = new Random();
5 // 创建ArrayList 对象
6 ArrayList<Integer> list = new ArrayList<>();
7 // 添加随机数到集合
8 for (int i = 0; i < 20; i++) {
9 int r = random.nextInt(1000) + 1;
10 list.add(r);
11 }
12 // 调用偶数集合的方法
13 ArrayList<Integer> arrayList = getArrayList(list);
14 System.out.println(arrayList);
15 }
16
17 public static ArrayList<Integer> getArrayList(ArrayList<Integer> list) {
18 // 创建小集合,来保存偶数
19 ArrayList<Integer> smallList = new ArrayList<>();
20 // 遍历list
21 for (int i = 0; i < list.size(); i++) {
22 // 获取元素
23 Integer num = list.get(i);
24 // 判断为偶数,添加到小集合中
25 if (num % 2 == 0){
26 smallList.add(num);
27 }
28 }
29 // 返回小集合
30 return smallList;
31 }
32 }
 1 public class Test04ArrayList {
2 public static void main(String[] args) {
3 // 创建Random 对象
4 Random random = new Random();
5 // 创建ArrayList 对象
6 ArrayList<Integer> list = new ArrayList<>();
7 // 添加随机数到集合
8 for (int i = 0; i < 20; i++) {
9 int r = random.nextInt(1000) + 1;
10 list.add(r);
11 }
12 // 调用偶数集合的方法
13 ArrayList<Integer> arrayList = getArrayList(list);
14 System.out.println(arrayList);
15 }
16
17 public static ArrayList<Integer> getArrayList(ArrayList<Integer> list) {
18 // 创建小集合,来保存偶数
19 ArrayList<Integer> smallList = new ArrayList<>();
20 // 遍历list
21 for (int i = 0; i < list.size(); i++) {
22 // 获取元素
23 Integer num = list.get(i);
24 // 判断为偶数,添加到小集合中
25 if (num % 2 == 0){
26 smallList.add(num);
27 }
28 }
29 // 返回小集合
30 return smallList;
31 }
32 }

11.2 String类

11.2.1 String的概述★★★

概述

java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例。类 String中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取子字符串以及创建具有翻译为大写或小写的所有字符的字符串的副本。

特点

1.字符串不变:字符串的值在创建后不能被更改。

1 String s1 = "abc";
2 s1 += "d";
3 System.out.println(s1);// "abcd"
4 // 内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。

2.因为String对象是不可变的,所以它们可以被共享。

1 String s1 = "abc";
2 String s2 = "abc";
3 // 内存中只有一个"abc"对象被创建,同时被s1和s2共享。

3."abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。

例如:
String str = "abc";
相当于:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
// String底层是靠字符数组实现的。

11.2.2 使用步骤★★★

  • 查看类

    • java.lang.String :此类不需要导入。

  • 查看构造方法

    • java.lang.String :此类不需要导入。 查看构造方法**

    • public String() :初始化新创建的 String对象,以使其表示空字符序列。

    • public String(char[] value) :通过当前参数中的字符数组来构造新的String。

    • public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的 String.

    • 构造举例,代码如下:

// 无参构造
String str = new String();
// 通过字符数组构造
char chars[] = {'a', 'b', 'c'};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = { 97, 98, 99 };
String str3 = new String(bytes);

11.2.3 常用方法 ★★★

判断功能的方法

  • public boolean equals (Object anObject) :将此字符串与指定对象进行比较。

  • public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小

    写。

方法演示,代码如下:

 1 public class String_Demo01 {
2 public static void main(String[] args) {
3 // 创建字符串对象
4 String s1 = "hello";
5 String s2 = "hello";
6 String s3 = "HELLO";
7 // boolean equals(Object obj):比较字符串的内容是否相同
8 System.out.println(s1.equals(s2));// true
9 System.out.println(s1.equals(s3)); // false
10 System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
11 //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
12 System.out.println(s1.equalsIgnoreCase(s2)); // true
13 System.out.println(s1.equalsIgnoreCase(s3)); // true
14 System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
15 }
16 }

Object 是” 对象”的意思,也是一种引用类型。作为参数类型,表示任意对象都可以传递到方法中。

获取功能的方法

  • public int length () :返回此字符串的长度。

  • public String concat (String str) :将指定的字符串连接到该字符串的末尾。

  • public char charAt (int index) :返回指定索引处的 char值。

  • public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。

  • public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。

  • public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。

方法演示,代码如下:

 1 public class String_Demo02 {
2 public static void main(String[] args) {
3 //创建字符串对象
4 String s = "helloworld";
5 // int length():获取字符串的长度,其实也就是字符个数
6 System.out.println(s.length());
7 System.out.println("‐‐‐‐‐‐‐‐");
8 // String concat (String str):将将指定的字符串连接到该字符串的末尾.
9 String s = "helloworld";
10 String s2 = s.concat("**hello jiyun");
11 System.out.println(s2);// helloworld**hello jiyun
12 // char charAt(int index):获取指定索引处的字符
13 System.out.println(s.charAt(0));
14 System.out.println(s.charAt(1));
15 System.out.println("‐‐‐‐‐‐‐‐");
16 // int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
17 System.out.println(s.indexOf("l"));
18 System.out.println(s.indexOf("owo"));
19 System.out.println(s.indexOf("ak"));
20 System.out.println("‐‐‐‐‐‐‐‐");
21 // String substring(int start):从start开始截取字符串到字符串结尾
22 System.out.println(s.substring(0));
23 System.out.println(s.substring(5));
24 System.out.println("‐‐‐‐‐‐‐‐");
25 // String substring(int start,int end):从start到end截取字符串。含start,不含end。
26 System.out.println(s.substring(0, s.length()));
27 System.out.println(s.substring(3,8));
28 }
29 }

转换功能的方法

  • public char[] toCharArray () :将此字符串转换为新的字符数组。

  • public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。

  • public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使

    用replacement字符串替换。

方法演示,代码如下:

 1 public class String_Demo03 {
2 public static void main(String[] args) {
3 //创建字符串对象
4 String s = "abcde";
5 // char[] toCharArray():把字符串转换为字符数组
6 char[] chs = s.toCharArray();
7 for(int x = 0; x < chs.length; x++) {
8 System.out.println(chs[x]);
9 }
10 System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
11 // byte[] getBytes ():把字符串转换为字节数组
12 byte[] bytes = s.getBytes();
13 for(int x = 0; x < bytes.length; x++) {
14 System.out.println(bytes[x]);
15 }
16 System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
17 // 替换字母it为大写IT
18 String str = "jiyun itedo";
19 String replace = str.replace("it", "IT");
20 System.out.println(replace);
21 // jiyun ITedo
22 System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
23 }
24 }

CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。

分割功能的方法

  • public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。

方法演示,代码如下:

 1 public class String_Demo03 {
2 public static void main(String[] args) {
3 //创建字符串对象
4 String s = "aa|bb|cc";
5 String[] strArray = s.split("|");
6 // ["aa","bb","cc"]
7 for(int x = 0; x < strArray.length; x++) {
8 System.out.println(strArray[x]);
9 // aa bb cc
10 }
11 }
12 }

11.2.4 String类的练习 ★★★★

拼接字符串

定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。格式参照如下:

 1 public class StringTest1 {
2 public static void main(String[] args) {
3 //定义一个int类型的数组
4 int[] arr = {1, 2, 3};
5 //调用方法
6 String s = arrayToString(arr);
7 //输出结果
8 System.out.println("s:" + s);
9 }
10 /*
11 * 写方法实现把数组中的元素按照指定的格式拼接成一个字符串
12 * 两个明确:
13 * 返回值类型:String
14 * 参数列表:int[] arr
15 */
16 public static String arrayToString(int[] arr) {
17 // 创建字符串s
18 String s = new String("[");
19 // 遍历数组,并拼接字符串
20 for (int x = 0; x < arr.length; x++) {
21 if (x == arr.length ‐ 1) {
22 s = s.concat(arr[x] + "]");
23 } else {
24 s = s.concat(arr[x] + "#");
25 }
26 }
27 return s;
28 }
29 }

统计字符个数

键盘录入一个字符串,统计字符串中大小写字母及数字字符个数

 1 public class StringTest2 {
2 public static void main(String[] args) {
3 //键盘录入一个字符串数据
4 Scanner sc = new Scanner(System.in);
5 System.out.println("请输入一个字符串数据:");
6 String s = sc.nextLine();
7 //定义三个统计变量,初始化值都是0
8 int bigCount = 0;
9 int smallCount = 0;
10 int numberCount = 0;
11 //遍历字符串,得到每一个字符
12 for(int x=0; x<s.length(); x++) {
13 char ch = s.charAt(x);
14 //拿字符进行判断
15 if(ch>='A'&&ch<='Z') {
16 bigCount++;
17 }else if(ch>='a'&&ch<='z') {
18 smallCount++;
19 }else if(ch>='0'&&ch<='9') {
20 numberCount++;
21 }else {
22 System.out.println("该字符"+ch+"非法");
23 }
24 }
25 //输出结果
26 System.out.println("大写字符:"+bigCount+"个");
27 System.out.println("小写字符:"+smallCount+"个");
28 System.out.println("数字字符:"+numberCount+"个");
29 }
30 }

四、本单元知识总结

1. ArrayList的定义
2. ArrayList存储数据
3. ArrayList遍历元素
4. String类的构造方法
5. String成员方法

最新文章

  1. 回首经典的SQL Server 2005
  2. 安装sass并ruby更改淘宝镜像
  3. 【转】数据库无关的GO语言ORM - hood
  4. 找到一款不错的网站压力测试工具webbench
  5. 解决Android解析图片的OOM问题!!!(转)
  6. 连续自然数和(codevs 1312)
  7. 删除sqlserver2008日记文件
  8. java与IOS之间的RSA加解密
  9. C# ToString格式大全
  10. Mac下Sublime快捷键
  11. jQuery版本引发的血案 iframe error 和 checkbox 无法勾选
  12. docker-跨主机存储
  13. &lt;笔记&gt;三码合一
  14. 《JAVA程序设计》_第一周学习总结
  15. Unix下5种I/O模型
  16. Scoop及使用
  17. Python判断字符集
  18. javascript基础学习系列-DOM盒子模型常用属性
  19. 201621123018《Java程序设计》第5周学习报告
  20. vue 获取后端数据打印结果undefined问题

热门文章

  1. Contos6.5卸载自带JDK
  2. Git pull and push
  3. 最新 .NET Core 中 WebSocket的使用 在Asp.Net MVC 中 WebSocket的使用 .NET Core 中 SignalR的使用
  4. Dapper同时操作任意多张表的实现
  5. mysql8.0----mysqldump抛出:Unknown table &#39;COLUMN_STATISTICS&#39; in information_schema (1109)
  6. 接口自动化-python+requests+pytest+csv+yaml
  7. Python面向对象编程及内置方法
  8. Python实现Thrift Server
  9. Pytest系列(19)- 我们需要掌握的allure特性
  10. 【机器学习|数学基础】Mathematics for Machine Learning系列之线性代数(1):二阶与三阶行列式、全排列及其逆序数