1.对象数组

1.1 对象数组概述

A:基本类型的数组:存储的元素为基本类型
int[] arr={1,2,3,4}
B:对象数组:存储的元素为引用类型
Student[] stus=new Student[3];
Student代表一个自定义类
stus数组中stus[0],stus[1],stus[2]的元素数据类型为Student,都可以指向一个Student对象

案例

  1. package com.igeek_03;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-25-14:21
  5. */
  6. import sun.management.counter.perf.PerfInstrumentation;
  7. /**
  8. * 数组:
  9. * 基本类型的数组--int,char····
  10. * 对象类型的数组--保存对象的数组--首先要有对象
  11. *
  12. * 当前这个学生类是一个实体类,用于后面的对象类型数组
  13. */
  14. public class Student {
  15. //属性
  16. private String name;
  17. private int age;
  18. //构造函数
  19. public Student() {
  20. }
  21. public Student(String name, int age) {
  22. this.name = name;
  23. this.age = age;
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. public int getAge() {
  32. return age;
  33. }
  34. public void setAge(int age) {
  35. this.age = age;
  36. }
  37. @Override
  38. public String toString() {
  39. return "Student{" +
  40. "name='" + name + '\'' +
  41. ", age=" + age +
  42. '}';
  43. }
  44. }
  1. package com.igeek_03;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-25-14:25
  5. */
  6. /**
  7. * 创建对象类型的数组---对象就是学生--Student类型的数组
  8. *
  9. * 做以下几件事情
  10. * 1.定义学生类
  11. * 2.创建学生类型的数组
  12. * 3.创建学生对象
  13. * 4.把学生对象存放进数组
  14. * 5.遍历学生数组
  15. */
  16. public class StudentDemo {
  17. public static void main(String[] args) {
  18. //2.创建学生类型的数组
  19. Student[] students=new Student[3];//长度为3
  20. //取值
  21. System.out.println(students[0]+" "+students[1]+" "+students[2]);
  22. System.out.println("----------------------");
  23. //把学生对象作为元素赋值给学生数组
  24. students[0]=new Student("王嘉尔",27);
  25. students[1]=new Student("王一博",23);
  26. students[2]=new Student("王俊凯",22);
  27. System.out.println(students[0]+" "+students[1]+" "+students[2]);
  28. //Student{name='王嘉尔', age=27} Student{name='王一博', age=23} Student{name='王俊凯', age=22}
  29. //遍历数组
  30. for (int i = 0; i <students.length ; i++) {
  31. //从数组中拿到一个学生对象就实例化
  32. Student s=students[i];
  33. System.out.println(s);
  34. // System.out.println(students[i]);
  35. System.out.println(s.getName()+" "+s.getAge());
  36. }
  37. }
  38. }

2.集合之ArrayList

Java中的集合:
其实集合与数组作用类似,就是用于同时保存和处理多个数据
但是数组一旦声明长度,就无法改变;
集合可以自动的变换长度,更加的灵活,可以同时保持不同类型的数组
—性能是次于数组的

ArrayList是数组类型的集合//属于util包下的
特点:
1.长度可变
2.可以保存不同类型的数据—前提是没有泛型

泛型:是jdk5.0版本推出的
作用:为了规范集合保存数据的类型,可以使用泛型进行约束

ArrayList添加新元素

  1. package com.igeek_05;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-25-14:40
  5. */
  6. import java.util.ArrayList;
  7. /**
  8. * Java中的集合:
  9. * 其实集合与数组作用类似,就是用于同时保存和处理多个数据
  10. * 但是数组一旦声明长度,就无法改变;
  11. * 集合可以自动的变换长度,更加的灵活,可以同时保持不同类型的数组
  12. * --性能是次于数组的
  13. *
  14. * ArrayList是数组类型的集合//属于util包下的
  15. * 特点:
  16. * 1.长度可变
  17. * 2.可以保存不同类型的数据--前提是没有泛型
  18. *
  19. * 泛型:是jdk5.0版本推出的
  20. * 作用:为了规范集合保存数据的类型,可以使用泛型进行约束
  21. */
  22. public class ArrayListDemo1 {
  23. public static void main(String[] args) {
  24. //演示集合的使用
  25. //1.创建对象
  26. ArrayList array=new ArrayList();
  27. //2.添加数据--任意的数据--add()方法
  28. array.add("Lynn");//String类型
  29. array.add(18);//int类型
  30. array.add(true);
  31. array.add(3.1415926535);
  32. //输出结果
  33. System.out.println(array);
  34. System.out.println("------------------");
  35. //演示泛型的使用
  36. ArrayList<String> list=new ArrayList<String>();
  37. //添加数据
  38. list.add("吴奇隆");
  39. list.add("苏有朋");
  40. list.add("陈志朋");
  41. //list.add(123);//会报错,因为只能输出字符串类型
  42. //输出
  43. System.out.println(list);
  44. }
  45. }

ArrayList删改查方法

A:获取元素
public E get(int index):返回指定索引处的元素
B:集合长度
public int size():返回集合中的元素的个数
C:删除元素
public boolean remove(Object o):删除指定的元素,返回删除是否成功
public E remove(int index):删除指定索引处的元素,返回被删除的元素
D:修改元素
public E set(int index,E element):修改指定索引处的元素,返回被修改的元素

  1. package com.igeek_05;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-25-15:12
  5. */
  6. import java.util.ArrayList;
  7. /**
  8. * ArrayList集合的删改查
  9. * 1.获取元素:E get(int index)--返回指定索引处的元素
  10. * 2.集合长度:int size()--返回一个集合中元素的个数
  11. * 3.删除元素:boolean remove(Object obj)--删除指定的元素,返回的是一个Boolean值
  12. * E remove(int index)--删除指定索引处的元素,返回的是删除的元素
  13. * 4.修改元素:E set(int index,E e)--修改指定索引处的元素,返回的是被修改的元素
  14. */
  15. public class ArrayListDemo2 {
  16. public static void main(String[] args) {
  17. //创建集合对象
  18. ArrayList<String> list=new ArrayList<String>();
  19. //添加元素
  20. list.add("刘德华");
  21. list.add("郭富城");
  22. list.add("黎明");
  23. list.add("张学友");
  24. //获取元素
  25. System.out.println("get:"+list.get(0));//get:刘德华
  26. //删除元素
  27. // System.out.println("remove:"+list.remove(1));//remove:郭富城
  28. // System.out.println("remove:"+list.remove("黎明"));//remove:true
  29. //查看集合
  30. System.out.println(list);
  31. //修改元素
  32. list.set(0,"刘芙蓉");
  33. // System.out.println("set:"+list.set(0,"刘芙蓉"));
  34. System.out.println(list);
  35. }
  36. }

ArrayList遍历

集合的遍历思想和数组的遍历思想相同
循环遍历容器,依次取出里面的元素即可

  1. package com.igeek_05;
  2. import java.util.ArrayList;
  3. /**
  4. * @author Lynn
  5. * @create 2020-11-25-15:30
  6. */
  7. /**
  8. * ArrayList集合的遍历
  9. * 与数组的遍历一样,长度改成size
  10. */
  11. public class ArrayListDemo3 {
  12. public static void main(String[] args) {
  13. //创建集合对象
  14. ArrayList<String> list=new ArrayList<String>();
  15. list.add("Tom");
  16. list.add("Lynn");
  17. list.add("马宝国");
  18. for (int i = 0; i <list.size() ; i++) {
  19. //获取下标所对应的元素
  20. System.out.println(list.get(i));
  21. }
  22. }
  23. }

ArrayList集合案例

ArrayList练习之存储字符串并遍历

向集合中添加任意四个字符串,遍历集合,依次打印取出的字符串

  1. package com.igeek_03;
  2. import java.util.ArrayList;
  3. /**
  4. * @ClassName: ArrayListTest
  5. * @Description: 存储字符串并遍历
  6. * @date 2017年10月14日 上午10:09:10
  7. * Company www.igeekhome.com
  8. *
  9. * 存储字符串并遍历
  10. *
  11. * 分析:
  12. * A:创建集合对象
  13. * B:添加字符串元素
  14. * C:遍历集合
  15. */
  16. public class ArrayListTest {
  17. public static void main(String[] args) {
  18. //创建集合对象
  19. ArrayList<String> array = new ArrayList<String>();
  20. //添加字符串元素
  21. array.add("向问天");
  22. array.add("刘正风");
  23. array.add("左冷禅");
  24. array.add("风清扬");
  25. //遍历集合
  26. for(int x=0; x<array.size(); x++) {
  27. String s = array.get(x);
  28. System.out.println(s);
  29. }
  30. }
  31. }

ArrayList练习之获取满足要求的元素

给定一个字符串数组:{“张三丰”,“宋远桥”,“张无忌”,“殷梨亭”,“张翠山”,“莫声谷”},将数组中的元素添加到集合中,并把所有姓张的人员打印到控制台上

  1. package com.igeek_03;
  2. import java.util.ArrayList;
  3. /**
  4. * @ClassName: ArrayListTest2
  5. * @Description: ArrayList练习之获取满足要求的元素
  6. * @date 2017年10月14日 上午10:11:10
  7. * Company www.igeekhome.com
  8. *
  9. * 给定一个字符串数组:{“张三丰”,“宋远桥”,“张无忌”,“殷梨亭”,“张翠山”,“莫声谷”},
  10. * 将数组中的元素添加到集合中,并把所有姓张的人员打印到控制台上。
  11. *
  12. * 分析:
  13. * A:定义字符串数组
  14. * B:创建集合对象
  15. * C:遍历字符串数组,获取到每一个字符串元素
  16. * D:把获取到的字符串元素添加到集合
  17. * E:遍历集合
  18. * 要判断每一个字符串元素是否以"张"开头,如果是,就输出在控制台
  19. */
  20. public class ArrayListTest2 {
  21. public static void main(String[] args) {
  22. //定义字符串数组
  23. String[] strArray = {"张三丰","宋远桥","张无忌","殷梨亭","张翠山","莫声谷"};
  24. //创建集合对象
  25. ArrayList<String> array = new ArrayList<String>();
  26. //遍历字符串数组,获取到每一个字符串元素
  27. for(int x=0; x<strArray.length; x++) {
  28. //把获取到的字符串元素添加到集合
  29. array.add(strArray[x]);
  30. }
  31. //遍历集合
  32. for(int x=0; x<array.size(); x++) {
  33. String s = array.get(x);
  34. //要判断每一个字符串元素是否以"张"开头,如果是,就输出在控制台
  35. if(s.startsWith("张")) {
  36. System.out.println(s);
  37. }
  38. }
  39. }
  40. }

ArrayList练习之存储自定义对象并遍历

A:自定义一个学生类,学生中有姓名和年龄属性,生成满参构造与空参构造
生成属性对应的getter/setter方法
B:在测试类中使用满参构造创建三个学生对象,然后将每个学生对象均添加到ArrayList集合中
C:遍历这个ArrayList集合,依次打印出每个学生的姓名和年龄

  1. package com.igeek_06;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-25-14:21
  5. */
  6. import sun.management.counter.perf.PerfInstrumentation;
  7. /**
  8. * 当前这个学生类是一个实体类
  9. */
  10. public class Student {
  11. //属性
  12. private String name;
  13. private String age;
  14. //构造函数
  15. public Student() {
  16. }
  17. public Student(String name, String age) {
  18. this.name = name;
  19. this.age = age;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public String getAge() {
  28. return age;
  29. }
  30. public void setAge(String age) {
  31. this.age = age;
  32. }
  33. @Override
  34. public String toString() {
  35. return "Student{" +
  36. "name='" + name + '\'' +
  37. ", age=" + age +
  38. '}';
  39. }
  40. }
  1. package com.igeek_06;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-25-15:35
  5. */
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8. /**
  9. * 键盘录入数据并且存储和遍历
  10. *
  11. * 创建一个学生类的集合,遍历学生集合
  12. *
  13. * 分析:
  14. * 1.定义学生类
  15. * 2.创建集合对象
  16. * 3.键盘录入数据,创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
  17. * 4.把学生对象存放在集合中
  18. * 5.遍历集合
  19. */
  20. public class StudentDemo {
  21. public static void main(String[] args) {
  22. //2.创建集合对象--泛型是学生类型
  23. ArrayList<Student> array=new ArrayList<Student>();
  24. //调用方法
  25. addStudent(array);
  26. addStudent(array);
  27. addStudent(array);
  28. //遍历集合
  29. for (int i = 0; i < array.size() ; i++) {
  30. Student s=array.get(i);
  31. System.out.println(s.getName()+" "+s.getAge());
  32. }
  33. }
  34. //定义一个方法用于封装添加学生
  35. //建议修饰符使用private,因为这个方法是只在当前这个类中使用,为了封装的彻底性
  36. public static void addStudent(ArrayList<Student> array){
  37. //键盘录入数据,创建成对象,把键盘录入的数据赋值给学生对象的成员变量
  38. Scanner sc=new Scanner(System.in);
  39. System.out.println("请输入学生的姓名:");
  40. String name=sc.nextLine();
  41. System.out.println("请输入学生的年龄:");
  42. String age=sc.nextLine();
  43. //创建学生对象
  44. Student s=new Student();
  45. //属性赋值
  46. s.setName(name);//name是获取到的用户输入的名字
  47. s.setAge(age);
  48. //把学生对象放进集合中保存
  49. array.add(s);
  50. // sc.close();
  51. }
  52. }

练习

  1. package practice;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-25-16:12
  5. */
  6. /**
  7. * 实现1注册,2登录,3退出
  8. */
  9. public class User {
  10. //定义成员变量
  11. private String name;
  12. private String birth;
  13. private String hobby;
  14. private String password;
  15. //构造方法
  16. public User() {
  17. }
  18. public User(String name, String birth, String hobby, String password) {
  19. this.name = name;
  20. this.birth = birth;
  21. this.hobby = hobby;
  22. this.password = password;
  23. }
  24. //get/set
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. public String getBirth() {
  32. return birth;
  33. }
  34. public void setBirth(String birth) {
  35. this.birth = birth;
  36. }
  37. public String getHobby() {
  38. return hobby;
  39. }
  40. public void setHobby(String hobby) {
  41. this.hobby = hobby;
  42. }
  43. public String getPassword() {
  44. return password;
  45. }
  46. public void setPassword(String password) {
  47. this.password = password;
  48. }
  49. @Override
  50. public String toString() {
  51. return "User{" +
  52. "name='" + name + '\'' +
  53. ", birth='" + birth + '\'' +
  54. ", hobby='" + hobby + '\'' +
  55. ", password='" + password + '\'' +
  56. '}';
  57. }
  58. }
  1. package practice;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4. /**
  5. * @author Lynn
  6. * @create 2020-11-25-16:12
  7. */
  8. public class Test {
  9. public static void main(String[] args) {
  10. //创建集合对象存放User数据
  11. ArrayList<User> list=new ArrayList<User>();
  12. Scanner sc=new Scanner(System.in);
  13. while(true){
  14. System.out.println("1注册,2登录,3退出");
  15. int num=sc.nextInt();
  16. if(num==1){
  17. register(list);
  18. }
  19. else if(num==2){
  20. login(list);
  21. }
  22. else if(num==3){
  23. System.out.println("谢谢使用!");
  24. System.exit(0);
  25. }
  26. else {
  27. System.out.println("输入方式有误!");
  28. }
  29. }
  30. }
  31. //定义register方法
  32. public static void register(ArrayList<User> list){
  33. Scanner sc=new Scanner(System.in);
  34. System.out.println("请输入你的用户名:");
  35. String name=sc.nextLine();
  36. System.out.println("请输入登录密码:");
  37. String password=sc.nextLine();
  38. System.out.println("请输入你的出生日期:");
  39. String birth=sc.nextLine();
  40. System.out.println("请输入你的爱好;");
  41. String hobby=sc.nextLine();
  42. //创建用户对象
  43. User u=new User();
  44. //属性赋值
  45. u.setName(name);
  46. u.setPassword(password);
  47. u.setBirth(birth);
  48. u.setHobby(hobby);
  49. //将用户对象保存在集合中
  50. list.add(u);
  51. System.out.println("注册成功!");
  52. }
  53. //定义login方法
  54. public static void login(ArrayList<User> list){
  55. Scanner sc=new Scanner(System.in);
  56. System.out.println("请输入你的用户名:");
  57. String name=sc.nextLine();
  58. System.out.println("请输入登录密码:");
  59. String password=sc.nextLine();
  60. User u=new User();
  61. u.setName(name);
  62. u.setPassword(password);
  63. //进行判断
  64. boolean flag=false;
  65. for (int i = 0; i < list.size() ; i++) {
  66. if(list.get(i).getName().equals(u.getName()) && list.get(i).getPassword().equals(u.getPassword())){
  67. System.out.println("以下是你的个人信息"+list.get(i));
  68. flag=true;
  69. }
  70. }
  71. if(!flag){
  72. System.out.println("你的用户名或者密码输入错误!");
  73. }
  74. }
  75. }

学生管理系统案例实现

  1. package practice03;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-26-9:31
  5. */
  6. /**
  7. * 这是一个学生的实体类
  8. */
  9. public class Student {
  10. //属性--为了方便获取字符串的信息,统一设置为字符串类型
  11. private String id;
  12. private String name;
  13. private String age;
  14. private String address;
  15. public Student() {
  16. }
  17. public Student(String id, String name, String age, String address) {
  18. this.id = id;
  19. this.name = name;
  20. this.age = age;
  21. this.address = address;
  22. }
  23. public String getId() {
  24. return id;
  25. }
  26. public void setId(String id) {
  27. this.id = id;
  28. }
  29. public String getName() {
  30. return name;
  31. }
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35. public String getAge() {
  36. return age;
  37. }
  38. public void setAge(String age) {
  39. this.age = age;
  40. }
  41. public String getAddress() {
  42. return address;
  43. }
  44. public void setAddress(String address) {
  45. this.address = address;
  46. }
  47. }
  1. package practice03;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-26-9:28
  5. */
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8. /**
  9. * 这是一个学生管理系统
  10. * 步骤:
  11. * 1.定义一个学生类
  12. * 2.学生管理系统主界面的编写
  13. * 3.学生管理系统的查看所有学生的信息
  14. * 4.添加学生信息
  15. * 5.删除学生信息
  16. * 6.修改学生信息
  17. */
  18. public class StudentMangerTest {
  19. public static void main(String[] args) {
  20. //定义一个学生类型集合
  21. ArrayList<Student> array=new ArrayList<Student>();
  22. //为了用户体验度,设计一个界面
  23. //利用循环的方式可以重复的展示界面,直到退出程序纠结束循环
  24. while(true){
  25. //主界面的欢迎词
  26. System.out.println("---------------------欢迎来到学生管理系统------------------------");
  27. System.out.print("1 查看所有学生"+"\t");
  28. System.out.print("2 添加学生"+"\t");
  29. System.out.print("3 删除学生"+"\t");
  30. System.out.print("4 修改学生"+"\t");
  31. System.out.print("5 退出系统"+"\t");
  32. System.out.println();
  33. //键盘录入获取
  34. Scanner sc=new Scanner(System.in);
  35. String choiceString=sc.nextLine();
  36. //用开关键--switch···case
  37. switch (choiceString){
  38. case "1":
  39. //查看所有学生
  40. findAllAtudent(array);
  41. break;
  42. case "2":
  43. //添加学生
  44. addStudent(array);
  45. break;
  46. case "3":
  47. //删除学生
  48. deleteStudent(array);
  49. break;
  50. case "4":
  51. //修改学生
  52. updateStudent(array);
  53. break;
  54. case "5":
  55. //退出系统
  56. default:
  57. System.out.println("谢谢使用,欢迎下次再来!");
  58. System.exit(0);//Java中提供的
  59. break;
  60. }
  61. }
  62. }
  63. //封装一个方法用于查询学生的所有信息
  64. public static void findAllAtudent(ArrayList<Student> array){
  65. //首先来判断集合中是否有数据,如果没有数据,就给出提示,并让该方法不继续往下执行
  66. if(array.size()==0){
  67. System.out.println("抱歉!目前没有学生信息可供查询,请回去重新选择你的操作");
  68. return;//将当前的方法直接结束
  69. }
  70. //有学生的情况下--遍历查询
  71. //\t表示一个Tab键--制表符
  72. System.out.println("学号\t姓名\t年龄\t居住地");
  73. for (int i = 0; i < array.size() ; i++) {
  74. Student s= array.get(i);
  75. //输出属性
  76. System.out.println(s.getId()+"\t"+
  77. s.getName()+"\t"+
  78. s.getAge()+"\t"+
  79. s.getAddress());
  80. }
  81. }
  82. //封装一个方法用于添加学生信息
  83. public static void addStudent(ArrayList<Student> array){
  84. //键盘录入对象
  85. Scanner sc=new Scanner(System.in);
  86. //所有的对学生的操作都是基于id,所以我们把id属性放到循环的外面
  87. String id;
  88. //能够实现重复添加学生的信息,所以使用循环
  89. while (true){
  90. //判断一下这个学号是不是已经存在
  91. System.out.println("请输入学号:");
  92. id=sc.nextLine();
  93. //定义一个标记--假设不存在
  94. boolean flag=false;
  95. //遍历学号--就是遍历数组
  96. for (int i = 0; i < array.size() ; i++) {
  97. Student s= array.get(i);
  98. //获取学号
  99. if(s.getId().equals(id)){
  100. flag=true;//学号存在
  101. break;
  102. }
  103. }
  104. //这个时候flag就是true
  105. if(flag){
  106. System.out.println("你的学号已经被占用,请重新输入!");
  107. }else {
  108. break;
  109. }
  110. }
  111. //通过验证可以添加学生
  112. System.out.println("请输入学生姓名:");
  113. String name=sc.nextLine();
  114. System.out.println("请输入学生年龄:");
  115. String age=sc.nextLine();
  116. System.out.println("请输入学生住址:");
  117. String address=sc.nextLine();
  118. //创建学生对象
  119. Student s=new Student();
  120. //赋值
  121. s.setId(id);
  122. s.setName(name);
  123. s.setAge(age);
  124. s.setAddress(address);
  125. //把学生保存到集合中
  126. array.add(s);
  127. //给出提示
  128. System.out.println("添加学生成功!");
  129. }
  130. //封装一个方法用于删除学生信息
  131. public static void deleteStudent(ArrayList<Student> array) {
  132. //删除学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就删除该学生
  133. //创建键盘录入对象
  134. Scanner sc=new Scanner(System.in);
  135. System.out.println("请输入你要删除的学生的学号:");
  136. String id=sc.nextLine();
  137. //遍历集合看看这个id所对应的学生是不是存在--设置一个索引
  138. int index=-1;
  139. for (int i = 0; i < array.size() ; i++) {
  140. //获取每一个学生
  141. Student s= array.get(i);
  142. //判断学生的学号和键盘录入的id进行比较
  143. if(s.getId().equals(id)){//要删除的对象存在
  144. index=i;
  145. break;
  146. }
  147. }
  148. if(index==-1){
  149. System.out.println("抱歉!你要删除的学号对应的学生信息不存在,请回去重新你的选择");
  150. }else {
  151. array.remove(index);
  152. System.out.println("删除成功!");
  153. }
  154. }
  155. //封装一个方法用于修改学生信息
  156. public static void updateStudent(ArrayList<Student> array){
  157. //修改学生的思路:键盘录入一个学号,到集合中去查找,看是否有学生使用的是该学号,如果有就修改该学生
  158. //创建键盘录入对象
  159. Scanner sc=new Scanner(System.in);
  160. System.out.println("请输入你要修改的学生的学号:");
  161. String id=sc.nextLine();
  162. //遍历集合看看这个id所对应的学生是不是存在--设置一个索引
  163. int index=-1;
  164. for (int i = 0; i < array.size() ; i++) {
  165. //获取每一个学生
  166. Student s= array.get(i);
  167. //判断学生的学号和键盘录入的id进行比较
  168. if(s.getId().equals(id)){//要修改的对象存在
  169. index=i;
  170. break;
  171. }
  172. }
  173. if(index==-1){
  174. System.out.println("抱歉!你要修改的学号对应的学生信息不存在,请回去重新你的选择");
  175. }else {
  176. //开始进行修改
  177. System.out.println("请输入新的学生姓名:");
  178. String name=sc.nextLine();
  179. System.out.println("请输入新的学生年龄:");
  180. String age=sc.nextLine();
  181. System.out.println("请输入新的学生住址:");
  182. String address=sc.nextLine();
  183. //创建一个新的对象
  184. Student s=new Student();
  185. s.setId(id);
  186. s.setName(name);
  187. s.setAge(age);
  188. s.setAddress(address);
  189. //修改集合中的学生对象--实际上就是把原来的信息替换
  190. array.set(index,s);
  191. System.out.println("修改成功!");
  192. }
  193. }
  194. }