为什么需要集合
- 数组是不可变长度的 在初始化时候就需要定义长度
- int[] arr=new int[10];
- 具体的直接看个简易的学生管理系统:
class User {
private String username;
private String sex;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [username=" + username + ", sex=" + sex + "]";
}
public User(String username, String sex) {
this.username = username;
this.sex = sex;
}
public User() {
}
}
public class ForObject {
public static void main(String[] args) throws IOException {
List<User> list=new ArrayList<User>();
while(true){
System.out.println("\t\t欢迎来到学生管理系统");
System.out.println("\t\t按1查看学生信息 ");
System.out.println("\t\t按2添加学生信息 ");
System.out.println("\t\t按3修改学生信息 ");
System.out.println("\t\t按4删除学生信息 ");
System.out.println("\t\t按5退出");
Scanner sc = new Scanner(System.in);
int per = sc.nextInt();
switch(per){
case 1:
showPerson(list);
break;
case 2:
list.add(getPerson());
break;
case 3:
updatePerson(list);
break;
case 4:
deletePerson(list);
break;
case 5:
System.out.println("退出系统");
System.exit(0);
break;
default:
break;
}
}
}
public static User getPerson(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名");
String username = sc.nextLine();
System.out.println("请输入性别");
String sex = sc.nextLine();
return new User(username,sex);
}
public static void showPerson(List<User> list){
for (User user : list) {
System.out.println(user);
}
}
public static void deletePerson(List<User> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名");
String username = sc.nextLine();
User entity=new User();
for (User user : list) {
if(user.getUsername().equals(username)){
entity=user;
//这里不能直接list.remove(user)会报并发修改异常,如果用for(int i=0;i<list.size();i++)或者while可以删除
//增强for是以迭代器为底层原理实现的。每次循环会和Collection进行比较。多了少了修改了都会报出此异常
//所有实现Iterator的都可以使用增强for
}
}
list.remove(entity);
}
public static void updatePerson(List<User> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生姓名");
String username = sc.nextLine();
User entity=new User();
for (User user : list) {
if(user.getUsername().equals(username)){
entity=user;
}
}
if(null==entity.getUsername()){
System.out.println("你输入的学生姓名不存在");
}else{
System.out.println("请输入新的学生姓名");
String newUserName = sc.nextLine();
System.out.println("请输入新的性别");
String newSex = sc.nextLine();
entity.setUsername(newUserName);
entity.setSex(newSex);
}
}
}
//list的删除需要注意很多细节 上面的foreach删除会报错,下面的例子:
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
for (int i = 0; i < list.size(); i++) {
list.remove(i);
}
System.out.println(list);//b、d
//这个为什么会出现这种情况呢?第一次循环删除掉A,然后集合还剩下bcd,然后i=1,删除掉c。然后i=2,list.size()==2退出循环
//这种的解决方案:
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
for (int i = list.size()-1; i >=0; i--) {//list索引最大是list.size()-1,最小的索引是0
list.remove(i);
}
System.out.println(list);
//总结:arraylist底层是数组,从中间删除索引,后面的数据会进行位置替换
}