前言
b站java课程学习笔记整理。
b站视频: 黑马程序员全套Java教程_Java基础入门视频教程,零基础小白自学Java必备教程
134. ArrayList
编程时如果想让数组大小可以变化,那么就可以选择集合类。ArrayList
就是一种集合类。(可以实现数组的插入元素等)
135.ArrayList构造与添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package ArrayList的构造与添加;import java.util.ArrayList;public class ArrayListAdd { public static void main (String[] args) { ArrayList<String> array = new ArrayList<String>(); System.out.println(array); array.add("hello!" ); System.out.println(array); array.add(0 , "Sven" ); System.out.println(array); } }
135. ArrayList常见方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package ArrayList的构造与添加;import java.util.ArrayList;public class ArrayListAdd { public static void main (String[] args) { ArrayList<String> array = new ArrayList<String>(); array.add("hello!" ); array.add(0 , "Sven" ); System.out.println(array); System.out.println(array.get(0 )); System.out.println(array.size()); } }
添加、删除、替换、返回指定索引处元素、返回元素个数。
136. 存储字符串,并遍历
需求:创建一个存储字符串的集合,存储三个字符串,并遍历。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package ArrayList遍历;import java.util.ArrayList;public class ArrayListBianli { public static void main (String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("hello" ); al.add("world" ); al.add("nihao" ); bianli(al); } public static void bianli (ArrayList<String> line) { for (int i = 0 ; i < line.size(); i++){ String s = line.get(i); System.out.println(s); } } }
137. ArrayList存储学生对象,并遍历
需求:创建一个存储学生对象的集合,存储三个学生对象,实用程序在控制台遍历这个集合。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package 存储学生对象并遍历;public class Student { private String name; private int age; public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { if (age>=0 && age<= 120 ){ this .age = age; }else { this .age = -1 ; } } public Student () { System.out.println("请为该对象填写信息!" ); }; public Student (String name, int age) { this .name = name; this .age = age; }; public void show () { System.out.println("名字是" +name+",年龄是" +age); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package 存储学生对象并遍历;import java.util.ArrayList;public class StudentTest { public static void main (String[] args) { Student s1 = new Student("Jiangeng Sun" , 24 ); Student s2 = new Student("Tongze" , 24 ); Student s3 = new Student("Longyiwenzi" , 24 ); ArrayList<Student> al = new ArrayList<Student>(); al.add(s1); al.add(s2); al.add(s3); bianli(al); } public static void bianli (ArrayList<Student> al) { for (int i = 0 ; i < al.size(); i++) { Student element = al.get(i); System.out.println(element); System.out.println("------------" ); System.out.println(element.getName() + "," + element.getAge()); System.out.println(); } } }
138. ArrayList存储学生对象,并遍历升级版
需求:创建一个存储学生对象的集合,存储三个学生对象,实用程序在控制台遍历这个集合。学生的数据使用键盘录入。
注意:为了键盘录入方便,学生类中的年龄也应该定义为String
类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package 存储学生对象并遍历;public class Student { private String name; private String age; public String getName () { return name; } public void setName (String name) { this .name = name; } public String getAge () { return age; } public void setAge (String age) { this .age = age; } public Student () { }; public Student (String name, String age) { this .name = name; this .age = age; }; public void show () { System.out.println("名字是" +name+",年龄是" +age); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 package 存储学生对象并遍历;import java.util.ArrayList;import java.util.Scanner;public class StudentTest { public static void main (String[] args) { Scanner sc = new Scanner(System.in); ArrayList<Student> al = new ArrayList<Student>(); int count = 1 ; System.out.println("请依次输入学生的姓名和年龄,按#结束!" ); while (true ) { System.out.println("请输入第" + count + "个同学的姓名" ); String name = sc.nextLine(); if (name.equals("#" )) { System.out.println("正在遍历列表……" ); break ; } System.out.println("请输入第" + count + "个同学的年龄" ); String age = sc.nextLine(); if (age.equals("#" )) { System.out.println("请补充年龄后再按#退出!" ); System.out.println("请输入第" + count + "个同学的年龄" ); age = sc.nextLine(); } Student student = new Student(); student.setAge(age); student.setName(name); al.add(student); count++; } bianli(al); } public static void bianli (ArrayList<Student> al) { for (int i = 0 ; i < al.size(); i++) { Student element = al.get(i); System.out.println(element); System.out.println("------------" ); System.out.println(element.getName() + "," + element.getAge()); System.out.println(); } } }
139. 学生管理系统项目演示
140. 学生管理系统之学生类
快捷键:Alt+Insert
生成get
、set
方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 package 学生管理系统;public class Student { private String id; private String name; private String age; private String address; public Student () { } public Student (String id, String name, String age, String address) { this .id = id; this .name = name; this .age = age; this .address = address; } public String getName () { return name; } public String getAddress () { return address; } public String getAge () { return age; } public String getId () { return id; } public void setAge (String age) { this .age = age; } public void setName (String name) { this .name = name; } public void setAddress (String address) { this .address = address; } public void setId (String id) { this .id = id; } }
141. 学生管理系统之主界面
注意:System.exit(0)
是JVM程序退出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package 学生管理系统;import java.util.Scanner;public class StudentCanva { public static void main (String[] args) { while (true ){ System.out.println("-------欢迎来到学生管理系统-------" ); System.out.println("1 添加学生" ); System.out.println("2 删除学生" ); System.out.println("3 修改学生" ); System.out.println("4 查看所有学生" ); System.out.println("5 退出" ); System.out.println("请输入你的选择:" ); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); switch (input){ case "1" : System.out.println("添加学生成功!" ); break ; case "2" : System.out.println("删除学生成功!" ); break ; case "3" : System.out.println("修改学生成功!" ); break ; case "4" : System.out.println("查看学生成功!" ); break ; case "5" : System.out.println("退出成功!" ); System.exit(0 ); } } } }
142. 学生管理系统之添加学生
143. 学生管理系统之查看学生
字符串中\t
就相当于按了下tab
。
144. 学生管理系统之查看学生升级版
如果ArrayList
中没有数据,应该提示一下无信息。
145. 学生管理系统之删除学生
146. 学生管理系统之修改学生
147. 学生管理系统之学号不存在
假如学号不存在,删除和修改操作不执行。
147. 学生管理系统之学号重复
如果学号重复了,给出提示。
学生管理系统的完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 package 学生管理系统;public class Student { private String id; private String name; private String age; private String address; public Student () { } public Student (String id, String name, String age, String address) { this .id = id; this .name = name; this .age = age; this .address = address; } public String getName () { return name; } public String getAddress () { return address; } public String getAge () { return age; } public String getId () { return id; } public void setAge (String age) { this .age = age; } public void setName (String name) { this .name = name; } public void setAddress (String address) { this .address = address; } public void setId (String id) { this .id = id; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 package 学生管理系统;import java.util.ArrayList;import java.util.Scanner;public class StudentCanva { public static void main (String[] args) { ArrayList<Student> list = new ArrayList<Student>(); while (true ) { System.out.println("-------欢迎来到学生管理系统-------" ); System.out.println("1 添加学生" ); System.out.println("2 删除学生" ); System.out.println("3 修改学生" ); System.out.println("4 查看所有学生" ); System.out.println("5 退出" ); System.out.println("请输入你的选择:" ); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); switch (input) { case "1" : addInfo(list); break ; case "2" : remove(list); break ; case "3" : modify(list); break ; case "4" : show(list); break ; case "5" : System.out.println("退出成功!" ); System.exit(0 ); } } } public static ArrayList<Student> addInfo (ArrayList<Student> list) { Scanner sc = new Scanner(System.in); String id; while (true ){ System.out.println("请输入要添加的学生的学号:" ); id = sc.nextLine(); if (check(id,list)){ System.out.println("该学号已被占用,请重新添加新的学号。" ); }else { break ; } } System.out.println("请输入要添加的学生的名字:" ); String name = sc.nextLine(); System.out.println("请输入要添加的学生的年龄:" ); String age = sc.nextLine(); System.out.println("请输入要添加的学生的住址:" ); String address = sc.nextLine(); Student student = new Student(); student.setId(id); student.setName(name); student.setAge(age); student.setAddress(address); list.add(student); System.out.println("添加学生成功!" ); return list; } public static void show (ArrayList<Student> list) { if (list.size() > 0 ) { System.out.println("--------------------------------------------" ); System.out.println("学号\t\t\t姓名\t\t年龄\t\t地址" ); for (int i = 0 ; i < list.size(); i++) { Student element = list.get(i); System.out.println(element.getId() + "\t" + element.getName() + "\t" + element.getAge() + "岁\t\t" + element.getAddress()); } System.out.println("--------------------------------------------" ); System.out.println("查看学生成功!" ); } else { System.out.println("系统内无学生信息,请添加信息!" ); } } public static void remove (ArrayList<Student> list) { Scanner sc = new Scanner(System.in); boolean flag = false ; System.out.println("请输入想要删除的学生的学号!" ); String id = sc.nextLine(); for (int i = 0 ; i < list.size(); i++) { Student element = list.get(i); if (element.getId().equals(id)) { list.remove(i); System.out.println("删除学生成功!" ); flag = true ; break ; } } if (!flag) { System.out.println("该学号不存在!" ); } } public static void modify (ArrayList<Student> list) { Scanner sc = new Scanner(System.in); boolean flag = false ; System.out.println("请输入想要修改的学生的学号!" ); String id = sc.nextLine(); for (int i = 0 ; i < list.size(); i++) { Student element = list.get(i); if (element.getId().equals(id)) { System.out.println("请输入新的姓名!" ); String name = sc.nextLine(); element.setName(name); System.out.println("请输入新的年龄!" ); String age = sc.nextLine(); element.setAge(age); System.out.println("请输入新的地址!" ); String address = sc.nextLine(); element.setAddress(address); list.remove(i); list.add(i, element); System.out.println("修改学生成功!" ); flag = true ; break ; } } if (!flag) { System.out.println("该学号不存在!" ); } } public static boolean check (String id, ArrayList<Student> list) { boolean flag = false ; for (int i = 0 ; i < list.size(); i++) { Student element = list.get(i); if (element.getId().equals(id)){ flag = true ; break ; } } return flag; } }