用集合做数据的增删改查
来源:互联网 发布:不要在淘宝搜索血滴子 编辑:程序博客网 时间:2024/05/17 02:04
import java.util.ArrayList;
import java.util.List;
Student 类
public class Student {
private int id;
private String name;
private String number;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, String number) {
super();
this.id = id;
this.name = name;
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
集合做数据的增删改查
package cn.scxh.www.information;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class DataChange {
public List<Student> studentInformation = new ArrayList<Student>();
public Student checkInformation(int num){
int count = studentInformation.size();
for (int i = 0; i < count; i++) {
Student student1 = studentInformation.get(i);
if (student1.getId() == num) {
return student1;
}
}
return null;
}
public void addInformation(){
System.out.println("======添加学生信息操作=========");
Scanner scaner = new Scanner(System.in);
System.out.println("请输入id:");
int id = scaner.nextInt();
System.out.println("请输入姓名:");
String name = scaner.next();
System.out.println("请输入学号:");
String number = scaner.next();
Student stu = checkInformation(id);
if (stu==null) {
Student student = new Student(id,name,number);
studentInformation.add(student);
System.out.println("输入成功");
}else{
System.out.println("此人存在,输入失败");
}
}
public void deletInformation(){
System.out.println("======删除学生信息操作=========");
Scanner scaner = new Scanner(System.in);
System.out.println("请输入id:");
int id = scaner.nextInt();
Student stu = checkInformation(id);
if (stu!=null) {
studentInformation.remove(stu);
System.out.println("删除成功");
}else{
System.out.println("此人不在管理系统中");
}
}
public void selectInformation(){
Scanner scaner = new Scanner(System.in);
System.out.println("请输入id:");
int id = scaner.nextInt();
Student stu = checkInformation(id);
if (stu!=null) {
String stuName = stu.getName();
String stuNumber = stu.getNumber();
System.out.println("此学生的姓名是:"+stuName+" "+"此学生的学号是:"+stuNumber);
}else{
System.out.println("此人不在管理系统中");
}
}
public void selectAllStudentInformation(){
for (Iterator iterator = studentInformation.iterator(); iterator
.hasNext();) {
Student object = (Student) iterator.next();
System.out.println(object.getId()+" "+object.getName()+" "+object.getNumber());
}
}
public void exit(){
System.out.println("退出学生管理系统");
System.exit(0);
}
}
测试学生类
package cn.scxh.www.information;
import java.util.Scanner;
public class TestStudent {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎来到学生管理系统");
Scanner scInformation = new Scanner(System.in);
DataChange studentInfo = new DataChange();
while (true) {
System.out.println("请输入操作命令");
int num = scInformation.nextInt();
switch(num){
case 1:
studentInfo.addInformation();
break;
case 2:
studentInfo.deletInformation();
break;
case 3:
studentInfo.selectInformation();
break;
case 4:
studentInfo.selectAllStudentInformation();
break;
case 5:
studentInfo.exit();
}
}
}
}
- 用集合做数据的增删改查
- 集合的增删改查
- 数据的增删改查
- 数据的增删改查
- 集合增删查改
- python做简单的增删改查
- Mvc5做的增删改查
- 用MyBatis实现数据的增删改查
- mysql表数据的增删查改
- mybatis ---- 实现数据的增删改查
- MongoDB数据的增删改查
- sql 基础 ( 数据的增删改查 )
- MySQL表数据的增删改查
- html实现数据的增删查改
- MyBatis实现数据的增删查改
- SQLite数据的增删查改
- sql数据增删改查的封装
- 增删改查的数据访问
- LCD1.axf: Error: L6218E: Undefined symbol Image$$RO$$Base (referred from 2440.0)解决
- 近似串匹配问题
- 扩展系统功能——装饰模式(三):图形界面构件库的装饰模式解决方案
- 水仙花数字
- [Raspbian]SSH连接到RaspberryPi进行操控
- 用集合做数据的增删改查
- C++----自己实现的链表
- 如何顺时针打印一个二维矩阵
- Intellij IDEA创建Maven Web项目
- 剑指Offer :请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配
- ssoj1065小Y的炮cannon
- nyoj 找点
- 在实战《第一行代码》里关于Group Heap的问题
- php mysqli 扩展配置