Java学习笔记9
集合 List
ArrayList集合的使用:
创建String, StringBuilder, ArrayList类的对象, 打印对象名, 都没有看到地址值, 而是元素内容
1.构造方法 :
public ArrayList() : 创建一个空的集合容器
2.集合容器的创建 :
ArrayList list = new ArrayList();
可以添加任意类型数据,会导致集合内的数据类型不一致,处理时有问题。
解决:
1
| ArrayList<String> list = new ArrayList<>(); //jdk7开始右边<>内不用写了,自动填入前面<>的内容
|
<> : 泛型
使用泛型, 可以对集合中存储的数据, 进行类型限制
泛型中, 不允许编写基本数据类型
在集合中, 存储 整数, 小数, 字符… 这些数据,类型的方法:
————使用基本数据类型对应的包装类
1 2 3 4 5 6 7 8
| byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character
|
注:没有隐式转换,不需要考虑
ArrayList 常用成员方法 :
1.增 :
public boolean add(E e) : 将指定的元素添加到此列表的尾部 //永远返回true,一般不用数据接收
public void add(int index, E element) : 在指定索引位置, 添加对应的元素 (插队)
2.删
public E remove(int index) : 根据索引做删除, 返回被删除掉的元素
public boolean remove(Object o) : 根据元素做删除, 删除首次出现的,返回是否删除成功的状态
3.改
public E set(int index, E element) : 修改指定索引位置, 为对应的元素, 返回被覆盖掉的元素
4.查
public E get(int index) : 根据索引, 获取集合中的元素
public int size() : 返回集合中元素的个数
实际使用:
需求1:创建一个存储字符串的集合, 内部存储字符串元素
使用程序实现在控制台遍历该集合, 将字符串长度为3的输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ArrayList<String> list = new ArrayList<>();
list.add("abcdf"); list.add("sanio"); list.add("happy"); list.add("sad"); list.add("try");
for (int i = 0; i < list.size(); i++) { String s = list.get(i); if(s.length() == 3){ System.out.println(s); } }
|
需求2: 创建一个存储String的集合,内部存储(test,张三,李四,test,test)字符串
删除所有的test字符串,删除后,将集合剩余元素打印在控制台
1 2 3 4 5 6 7
| for (int i = 0; i < list.size(); i++) { String s = list.get(i); if("test".equals(s)){ list.remove(i); i--; } }
|
总结: 在遍历集合的过程中, 有删除操作
1. 正序遍历 : 不要忘记 --操作
2. 倒序遍历:不需要,正常遍历即可
1 2 3 4 5 6
| for(int i = list.size() - 1; i >= 0; i--){ String s = list.get(i); if("test".equals(s)){ list.remove(i); } }
|
注:变量和常量比较
要用常量.equals(变量); 否则变量.equals(常量)可能发生空指针异常;
学生管理系统的搭建——集合的增删改查
需求:实现学生信息(id, name, age,birthday)的增删改查,学号不允许重复。
Student类
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
| public class Student {
private String id; private String name; private int age; private String birthday; public Student() { }
public Student(String id, String name, int age, String birthday) { this.id = id; this.name = name; this.age = age; this.birthday = birthday; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getBirthday() { return birthday; }
public void setBirthday(String birthday) { this.birthday = birthday; }
}
|
StudentManage
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
| import java.util.ArrayList; import java.util.Scanner;
public class Test { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<>(); Scanner sc = new Scanner(System.in); 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("请输入您的选择:");
int choice = sc.nextInt(); switch (choice) { case 1: addStudent(list); break; case 2: deleteStudentById(list); break; case 3: updateStudentInfoById(list); break; case 4: queryStudentInfos(list); break; case 5: System.out.println("感谢您的使用, 再见"); System.exit(0); break; default: System.out.println("输入有误, 请检查"); break; } } }
private static void addStudent(ArrayList<Student> list) { Scanner sc = new Scanner(System.in); System.out.println("请输入学生学号:"); String id = ""; while (true) { id = sc.next(); int index = getIndex(id, list); if (index == -1) { break; } else { System.out.println("您输入的学号已被占用, 请重新输入!"); } }
System.out.println("请输入学生姓名:"); String name = sc.next(); System.out.println("请输入学生年龄:"); int age = sc.nextInt(); System.out.println("请输入学生生日:"); String birthday = sc.next();
Student stu = new Student(id, name, age, birthday); list.add(stu); System.out.println("添加成功!"); }
private static void updateStudentInfoById(ArrayList<Student> list) { Scanner sc = new Scanner(System.in); System.out.println("请输入要修改的学生学号:"); String updateId = sc.next(); int index = getIndex(updateId, list); if (index == -1) { System.out.println("查无此人, 修改失败!"); } else { System.out.println("请输入新的学生姓名:"); String name = sc.next(); System.out.println("请输入新的学生年龄:"); int age = sc.nextInt(); System.out.println("请输入新的学生生日:"); String birthday = sc.next();
Student stu = new Student(updateId, name, age, birthday); list.set(index, stu); System.out.println("修改成功!"); } }
private static void deleteStudentById(ArrayList<Student> list) { Scanner sc = new Scanner(System.in); System.out.println("请输入您要删除的学生学号:"); String id = sc.next(); int index = getIndex(id, list); if (index == -1) { System.out.println("查无此人, 删除失败!"); } else { list.remove(index); System.out.println("删除成功!"); }
}
private static void queryStudentInfos(ArrayList<Student> list) { if (list.size() == 0) { System.out.println("查无信息, 请重试!"); } else { System.out.println("学号\t\t\t姓名\t年龄\t生日"); for (int i = 0; i < list.size(); i++) { Student stu = list.get(i); System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday()); } } }
private static int getIndex(String id, ArrayList<Student> list) { for (int i = 0; i < list.size(); i++) { Student stu = list.get(i); if (stu.getId().equals(id)) { return i; } } return -1; } }
|
注:System.exit(0); // 终止正在运行的JVM虚拟机, 通常用0表示正常退出
效果同之前的lo标记循环再break lo;