Collection的使用(2):保存学生信息

package collectionStudy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Collection的使用:保存学生信息
 * @create 2021-02-25 9:21
 */
public class Demo02 {
    public static void main(String[] args) {
        //新建Collection对象
        Collection collection=new ArrayList();
        Student s1 = new Student("张三",20);
        Student s2 = new Student("罗斯",22);
        Student s3 = new Student("王二",10);
        //1添加数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println("元素个数:"+collection.size());
        System.out.println(collection.toString());
        //2删除
        collection.remove(s1);
        System.out.println("删除之后:"+collection.size());
        //3遍历
        //3.1增强for
        System.out.println("-------增强for-------");
        for (Object object:collection
             ) {Student s=(Student) object;
            System.out.println(s.toString());

        }
        //3.2迭代器:hasNext();next();remove();迭代过程中不能使用collection的删除方法
        System.out.println("-------迭代器-------");
        Iterator it =collection.iterator();
        while (it.hasNext()){
            Student s=(Student)it.next();
            System.out.println(s.toString());
        }
        //4判断
        System.out.println(collection.contains(s2));
        System.out.println(collection.isEmpty());
    }
}

学生类

package collectionStudy;

/**
 * 学生类
 * @create 2021-02-25 9:21
 */
public class Student {
    private  String name;
    private  int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
由于需要连接数据库,这里提供一个基本的代码框架,需要根据具体情况进行修改和完善。 ```javascript // 连接数据库 const db = connect('your_database_name'); // 获取增加、删除、修改按钮 const addButton = document.querySelector('#addButton'); const deleteButton = document.querySelector('#deleteButton'); const editButton = document.querySelector('#editButton'); // 给增加按钮添加点击事件 addButton.addEventListener('click', () => { // 弹出增加对话框 const studentName = prompt('请输入学生姓名'); const studentAge = prompt('请输入学生年龄'); const studentGender = prompt('请输入学生性别'); // 将学生信息保存到数据库 db.collection('students').insertOne({ name: studentName, age: studentAge, gender: studentGender }); }); // 给删除按钮添加点击事件 deleteButton.addEventListener('click', () => { // 弹出删除对话框 const studentId = prompt('请输入要删除的学生ID'); // 从数据库中删除学生信息 db.collection('students').deleteOne({ _id: ObjectId(studentId) }); }); // 给修改按钮添加点击事件 editButton.addEventListener('click', () => { // 弹出修改对话框 const studentId = prompt('请输入要修改的学生ID'); const studentName = prompt('请输入修改后的学生姓名'); const studentAge = prompt('请输入修改后的学生年龄'); const studentGender = prompt('请输入修改后的学生性别'); // 更新数据库中的学生信息 db.collection('students').updateOne({ _id: ObjectId(studentId) }, { $set: { name: studentName, age: studentAge, gender: studentGender } }); }); ``` 需要注意的是,这里使用了 `prompt` 弹出对话框,实际应用中可能需要使用更为美观的模态框。此外,由于涉及到数据库操作,建议在服务端使用 Node.js 编写代码,通过 API 接口提供服务,前端通过 AJAX 请求获取数据并更新页面。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值