java集合框架
Student类numbernameagesex4个属性定义为private,并用gettersetter方法提供外部访问声明一个ArrayList,循环创建5个学员对象...
Student类
number
name
age
sex
4个属性定义为private,并用getter setter 方法提供外部访问
声明一个ArrayList,循环创建5个学员对象保存在集合里
任意输入要查找的学员的编号,显示出该编号学员的姓名,如果该编号学员不存在,提示没有该编号学员
任意输入要删除的学员编号,如果该编号学员存在,删除该学员,提示删除成功,如果不存在,提示该学员不存在 展开
number
name
age
sex
4个属性定义为private,并用getter setter 方法提供外部访问
声明一个ArrayList,循环创建5个学员对象保存在集合里
任意输入要查找的学员的编号,显示出该编号学员的姓名,如果该编号学员不存在,提示没有该编号学员
任意输入要删除的学员编号,如果该编号学员存在,删除该学员,提示删除成功,如果不存在,提示该学员不存在 展开
2个回答
展开全部
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
class Student{
private int number;
private String name;
private int age;
private String sex;
public Student() {
number=0;
name=null;
age=0;
sex=null;
}
public Student(int number,String name,int age,String sex){
this.number =number;
this.name=name;
this.age=age;
this.sex=sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
public class Test5 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
Student[] stu=new Student[]{new Student(1,"Tom",15,"男"),
new Student(2,"Lucy",15,"女"),new Student(3,"Lily",14,"女"),
new Student(4,"Jack",16,"男"),new Student(5,"Kim",15,"男")};
for (int i = 0; i < stu.length; i++) {
list.add(stu[i]);
}
System.out.println("请输入要查找的学生编号:");
Scanner sca1=new Scanner(System.in);
int sca1_=sca1.nextInt();
for (Student student : stu) {
if(student.getNumber()==sca1_){
System.out.println(student.getName());
break;
}
else if (list.get(list.size()-1)==student) {
System.out.println("没有该编号学员");
}
}
System.out.println("请输入要删除的学生编号:");
sca1_=sca1.nextInt();
int index_;
for (Student student : stu) {
if(student.getNumber()==sca1_){
list.remove(list.indexOf(student));
System.out.println("删除成功");
break;
}
else if (list.get(list.size()-1)==student) {
System.out.println("没有该编号学员");
}
}
}
}
修订后的完全版哦,给好评哦亲
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询