题目要求:
定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。 学生的属性:学号,姓名,年龄.
要求1: 再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。
要求2:添加完毕之后,遍历所有学生信息。
要求3: 通过id删除学生信息 如果存在,则删除,如果不存在,则提示删除失败。
要求4:删除完毕之后,遍历所有学生信息
要求5:查询数组id为“heima002”的学生,如果存在,则 将他的年龄+1岁
解题思路:
1.创建一个长度为3的数组
2.创建学生对象
3.把学生对象添加到数组当中
4.再次创建一个学生对象
5.唯一性判断
5.1 已存在 ---提示重复
5.2 不存在 ---添加学生对象
6.添加学生对象
6.1 老数组已经存满
6.2 老数组没有存满
第一步我们先创建数组用来存储学生对象,我们先新建一个Student类,我们设置好成员变量后分别创建空参,全参还有get方法,set方法,此时标准的javaBead就创建完成了。
public class Student {
private int id ;//id
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
然后我们就可以创建一个数组来存储学生对象。
Student[] arr = new Student[3];
第二步创建学生对象并添加数组当中,学生对象的数量无所谓,题目中的要求是1-3个就可以,我们要注意的是我们添加的学生对象,姓名和学号别整成一样的就可以。
Student stu1 = new Student(1,"xiaowang",19);
Student stu2 = new Student(2,"xiaozhang",20);
Student stu3 = new Student(3,"xiaomao",21);
Student学生类
public class Student {
private int id;//学生的Id可能是字母。也可能是字符串
// 但是我们现在还没有学字符串的比较我们先把id设置成int类型
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试类
//定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,
// 姓名各不相同学生的属性:学号,姓名,年龄。
要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断.
要求2:添加完毕之后,遍历所有学生信息。
要求3:通过id删除学生信息
如果存在,则删除,如果不存在,则提示删除失败。
要求4: 删除完毕之后,遍历所有学生信息。
要求5:查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁
public class Test {
public static void main(String[] args) {
//1.创建一个数组用来存储学生对象,数组的类型我肯定是一个学生类,但是我们现在还没有创。所以
// 我们要先写一个Student类,属性学号,姓名,年龄
Student[] arr = new Student[3];
//2.创建学生对象,并添加到数组当中
Student stu1 = new Student(1,"wangwu",21);
Student stu2 = new Student(2,"zhaoliu",22);
Student stu3 = new Student(3,"liuba",23);
//3.把学生对象添加到数组当中
arr[0] = stu1;
arr[1] = stu2;
arr[2] = stu3;
//要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
//读题拆解法
Student stu4 = new Student(4,"liujiu",24);
//下一步要把stu4添加到数组当中
//唯一性判断
//已存在--不用添加
//不存在--就可以把学生对象添加到数组
boolean flag = contains(arr, stu4.getId());
//我们对boolean类型的值进行判断,我们直接写在圆括号中就行()圆括号
if(flag){
//表示id存在的
System.out.println("当前id重复,请修改id后添加");
}else {
//不存在就可以把学生对象添加到数组
int count = getconut(arr);
if(count == arr.length) {
//已经存满
Student[] newArr = creatNewArr(arr);
//[stu1,stu2,stu3]
//[stu1,stu2,stu3,null]
//最后一个空着的话,我可以把stu4添加进去就可以了
newArr[count] = stu4;
//要求2:添加完毕后,遍历所有学生信息
prinArr(newArr);
}
else {
//没有存满
arr[count] = stu4;
//还有一层意思:如果下一次要添加数据,就是添加到2索引的位置
prinArr(arr);
}
}
//把stu4添加到数组中
//1.数组已经存满---只能创建一个新的数组,新数组的长度=老数组+1
//2.数组没有存满---直接添加
//3.调用处是否
//1.我要干嘛? 唯一的判断
//2.我干这件事,需要什么才能完成?数组id
//调用处是否需要继续使用方法
//要求2:添加完毕后,遍历所有学生信息
}
public static void prinArr(Student[] arr){
for (int i = 0; i < arr.length; i++) {
Student stu = arr[i];
if(stu != null){
System.out.println(stu.getId()+","+ stu.getName()+","+ stu.getAge());
}
}
}
public static Student[] creatNewArr(Student[] arr){
Student[] newArr = new Student[arr.length+1];
//循环遍历得到老数组中的每一个元素
for (int i = 0; i < arr.length; i++) {
//把老数组的元素添加到新数组当中
newArr[i] = arr[i];
}
//把新数组返回
return newArr;
}
//定义一个方法判断数组中已经存了几个元素
public static int getconut(Student[] arr){
//定义一个计数器,用来统计
int count = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i] != null){
count++;
}
}
//当循环结束之后,我就知道了数组当中一共有几个元素
return count;
}
public static boolean contains(Student[] arr,int id){//1 2 3.....3是否存在
for(int i = 0; i < arr.length; i++) {
Student stu =arr[i];
if(stu!=null){
int sid = stu.getId();
//比较
if(sid == id){
return true;
}
}
}
//当循环结束之后,还没有找到一样的,那么就表示数组中要查找的id是不存在的
return false;
}
}
运行结果:
1,wangwu,21
2,zhaoliu,22
3,liuba,23
4,liujiu,24