[Detailed + super basic] Java-study notes 04

1.ArrayList

1.1 Overview of the ArrayList class [understanding]

What is a collection

Provide a storage model with variable storage space, and the stored data capacity can be changed

Characteristics of ArrayList collection

The bottom layer is implemented by an array, and the length can be changed

The use of generics is used to constrain the data type of the elements stored in the collection

1.2 Common methods of ArrayList class [application]

1.2.1 Construction method

Method name Description
public ArrayList() Create an empty collection object

1.2.2 Member Method

Method name Description
public boolean remove(Object o) Delete the specified element, return whether the deletion is successful
public E remove(int index) Delete the element at the specified index and return the deleted element
public E set(int index,E element) Modify the element at the specified index and return the modified element
public E get(int index) Returns the element at the specified index
public int size() Returns the number of elements in the collection
public boolean add(E e) Append the specified element to the end of this collection
public void add(int index,E element) Insert the specified element at the specified position in this collection

1.2.3 Sample code

public class ArrayListDemo02 {

public static void main(String[] args) {

//创建集合

ArrayList array = new ArrayList();

//添加元素

array.add("hello");

array.add("world");

array.add("java");

//public boolean remove(Object o):删除指定的元素,返回删除是否成功

// System.out.println(array.remove("world"));

// System.out.println(array.remove("javaee"));

//public E remove(int index):删除指定索引处的元素,返回被删除的元素

// System.out.println(array.remove(1));

//IndexOutOfBoundsException

// System.out.println(array.remove(3));

//public E set(int index,E element):修改指定索引处的元素,返回被修改的元素

// System.out.println(array.set(1,"javaee"));

//IndexOutOfBoundsException

// System.out.println(array.set(3,"javaee"));

//public E get(int index):返回指定索引处的元素

// System.out.println(array.get(0));

// System.out.println(array.get(1));

// System.out.println(array.get(2));

//System.out.println(array.get(3));

//public int size():返回集合中的元素的个数

System.out.println(array.size());

//输出集合

System.out.println("array:" + array); } }

1.3ArrayList stores strings and traverses [application]

1.3.1 Case requirements

Create a collection of stored strings, store 3 string elements, and use the program to traverse the collection in the console

1.3.2 Code Implementation

/* 思路:

1:创建集合对象

2:往集合中添加字符串对象

3:遍历集合,首先要能够获取到集合中的每一个元素,这个通过get(int index)方法实现

4:遍历集合,其次要能够获取到集合的长度,这个通过size()方法实现

5:遍历集合的通用格式 ``*/

public class ArrayListTest01 {

public static void main(String[] args) {

//创建集合对象 ArrayList array = new ArrayList();

//往集合中添加字符串对象

array.add("sun");

array.add("bu");

array.add("jian");

//遍历集合,其次要能够获取到集合的长度,这个通过size()方法实现

// System.out.println(array.size());

//遍历集合的通用格式 for(int i=0; i<array.size(); i++) { String s = array.get(i); System.out.println(s); } } }

1.4ArrayList stores student objects and traverses [application]

1.4.1 Case requirements

Create a collection to store student objects, store 3 student objects, and use the program to traverse the collection in the console

1.4.2 Code Implementation

/* 思路:

1:定义学生类

2:创建集合对象

3:创建学生对象

4:添加学生对象到集合中

5:遍历集合,采用通用遍历格式实现 */

public class ArrayListTest02 {

public static void main(String[] args) {

//创建集合对象 ArrayList array = new ArrayList<>();

//创建学生对象

Student s1 = new Student("孙不坚", 30);

Student s2 = new Student("彭于晏", 33);

Student s3 = new Student("陈伟霆", 18);

//添加学生对象到集合中

array.add(s1);

array.add(s2);

array.add(s3);

//遍历集合,采用通用遍历格式实现

for (int i = 0; i < array.size(); i++) {

Student s = array.get(i);

System.out.println(s.getName() + "," + s.getAge()); } }}

1.5ArrayList stores student objects and traverses the upgraded version [application]

1.5.1 Case requirements

Create a collection to store student objects, store 3 student objects, and use the program to traverse the collection on the console. The student’s name and age come from keyboard entry

1.5.2 Code Implementation

/* 思路:

1:定义学生类,为了键盘录入数据方便,把学生类中的成员变量都定义为String类型

2:创建集合对象

3:键盘录入学生对象所需要的数据

4:创建学生对象,把键盘录入的数据赋值给学生对象的成员变量

5:往集合中添加学生对象

6:遍历集合,采用通用遍历格式实现 */

public class ArrayListTest {

public static void main(String[] args) {

//创建集合对象 ArrayList array = new ArrayList();

//为了提高代码的复用性,我们用方法来改进程序

addStudent(array);

addStudent(array);

addStudent(array);

//遍历集合,采用通用遍历格式实现

for (int i = 0; i < array.size(); i++) {

Student s = array.get(i);

System.out.println(s.getName() + "," + s.getAge()); } }

/* 两个明确: 返回值类型:void

参数:ArrayList array */

public static void addStudent(ArrayList array) {

//键盘录入学生对象所需要的数据

Scanner sc = new Scanner(System.in);

System.out.println("请输入学生姓名:");

String name = sc.nextLine();

System.out.println("请输入学生年龄:");

String age = sc.nextLine();

//创建学生对象,把键盘录入的数据赋值给学生对象的成员变量

Student s = new Student();

s.setName(name);

s.setAge(age);

//往集合中添加学生对象

array.add(s); } }

2. Student Management System

2.1 Implementation steps of student management system [understanding]

Case requirements

Based on what we have learned so far, complete a comprehensive case: student management system! The main functions of the system are as follows:

Add student: Enter student information through the keyboard and add it to the collection

Delete student: Enter the student ID of the student to be deleted through the keyboard, and delete the student object from the collection

Modify student: Enter the student ID of the student to be modified by keyboard, and modify other information of the student object

View students: display the student object information in the collection

Exit the system: end the program

Implementation steps

A defines the student class, including the following member variables

​ private String sid // 学生id

​ private String name // student name

​ private String age // student age

​ private String address // student location

B. Steps to build the main interface of the student management system

a. Use output sentences to complete the preparation of the main interface

b. Use Scanner to achieve keyboard input

c. Use the switch statement to complete the selected function

d. Return to the main interface again after the end of the cycle completion function

Steps to realize the function of adding students in C student management system

a. Define a method to receive the ArrayList collection

b. Complete the function of adding students within the method

​ ①Keyboard input student information

​ ②Create student objects based on the entered information

​ ③Add the student object to the collection

​ ④Prompt to add successful information

c. Call the method of adding students in the option of adding students

D The realization steps of the student viewing function of the student management system

a. Define a method to receive the ArrayList collection

b. Traverse the collection within the method and output the student information

c. Call the view student method in the view all students option

E. The steps to implement the delete student function of the student management system

a. Define a method to receive the ArrayList collection

b. Receive the student ID to be deleted in the method

c. Traverse the collection to get each student object

d. Use the student ID of the student object and the entered student ID to be deleted for comparison, if the same, delete the current student object from the set

e. Call the method of deleting students in the delete student option

Steps to realize the function of modifying students in F student management system

a. Define a method to receive the ArrayList collection

b. In the method, the student ID of the student to be modified is accepted

c. Enter the information needed by the student object through the keyboard and create the object

d. Traverse the collection to obtain each student object. And compare it with the modified student ID entered. If the same, replace the current student object with the new student object

e. Call the method of modifying students in the option of modifying students

f. Use System.exit(0) to exit the system; exit JVM

2.2 Definition of student class [application]

public class Student {
    
    
//学号
private String sid;
//姓名
private String name;
//年龄
private String age;
//居住地
private String address;
public Student() {
    
    
}
public Student(String sid, String name, String age, String address) {
    
    
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
    
    
return sid;
}
public void setSid(String sid) {
    
    
this.sid = sid;
}
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 String getAddress() {
    
    
return address;
}
public void setAddress(String address) {
    
    
this.address = address;
}
}

2.3 Definition of test class [application]

public class StudentManager {
    
    
/*
1:用输出语句完成主界面的编写
2:用Scanner实现键盘录入数据
3:用switch语句完成操作的选择
4:用循环完成再次回到主界面
*/
public static void main(String[] args) {
    
    
//创建集合对象,用于保存学生数据信息
ArrayList<Student> array = 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实现键盘录入数据
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
//用switch语句完成操作的选择
switch (line) {
    
    
case "1":
addStudent(array);
break;
case "2":
deleteStudent(array);
break;
case "3":
updateStudent(array);
break;
case "4":
findAllStudent(array);
break;
case "5":
System.out.println("谢谢使用");
System.exit(0); //JVM退出
}
}
}
//定义一个方法,用于添加学生信息
public static void addStudent(ArrayList<Student> array) {
    
    
//键盘录入学生对象所需要的数据,显示提示信息,提示要输入何种信息
Scanner sc = new Scanner(System.in);
String sid;
while (true) {
    
    
System.out.println("请输入学生学号:");
sid = sc.nextLine();
boolean flag = isUsed(array, sid);
if (flag) {
    
    
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 s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//将学生对象添加到集合中
array.add(s);
//给出添加成功提示
System.out.println("添加学生成功");
}
//定义一个方法,判断学号是否被使用
public static boolean isUsed(ArrayList<Student> array, String sid) {
    
    
//如果与集合中的某一个学生学号相同,返回true;如果都不相同,返回false
boolean flag = false;
for(int i=0; i<array.size(); i++) {
    
    
Student s = array.get(i);
if(s.getSid().equals(sid)) {
    
    
flag = true;
break;
}
}
return flag;
}
//定义一个方法,用于查看学生信息
public static void findAllStudent(ArrayList<Student> array) {
    
    
//判断集合中是否有数据,如果没有显示提示信息
if (array.size() == 0) {
    
    
System.out.println("无信息,请先添加信息再查询");
//为了让程序不再往下执行,我们在这里写上return;
return;
}
//显示表头信息
//\t其实是一个tab键的位置
System.out.println("学号\t\t\t姓名\t\t年龄\t\t居住地");
//将集合中数据取出按照对应格式显示学生信息,年龄显示补充“岁”
for (int i = 0; i < array.size(); i++) {
    
    
Student s = array.get(i);
System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() +
"岁\t\t" + s.getAddress());
}
}
//定义一个方法,用于删除学生信息
public static void deleteStudent(ArrayList<Student> array) {
    
    
//键盘录入要删除的学生学号,显示提示信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学生的学号:");
String sid = sc.nextLine();
//在删除/修改学生操作前,对学号是否存在进行判断
//如果不存在,显示提示信息
//如果存在,执行删除/修改操作
int index = -1;
for (int i = 0; i < array.size(); i++) {
    
    
Student s = array.get(i);
if (s.getSid().equals(sid)) {
    
    
index = i;
break;
}
}
if (index == -1) {
    
    
System.out.println("该信息不存在,请重新输入");
} else {
    
    
array.remove(index);
//给出删除成功提示
System.out.println("删除学生成功");
}
}
//定义一个方法,用于修改学生信息
public static void updateStudent(ArrayList<Student> array) {
    
    
//键盘录入要修改的学生学号,显示提示信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生的学号:");
String sid = sc.nextLine();
//键盘录入要修改的学生信息
System.out.println("请输入学生新姓名:");
String name = sc.nextLine();
System.out.println("请输入学生新年龄:");
String age = sc.nextLine();
System.out.println("请输入学生新居住地:");
String address = sc.nextLine();
//创建学生对象
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//遍历集合修改对应的学生信息
for (int i = 0; i < array.size(); i++) {
    
    
Student student = array.get(i);
if (student.getSid().equals(sid)) {
    
    
array.set(i, s);
}
}
//给出修改成功提示
System.out.println("修改学生成功");
}
}

Guess you like

Origin blog.csdn.net/qq_51808107/article/details/113007129