Java学习复杂的对象数组操作
定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。
学生的属性:学号,姓名,年龄。
- 要求1:再次添加一个学生对象,并在添加的时候进行学* 号的唯一性判断。
- 要求2:添加完毕之后,遍历所有学生信息。
- 要求3:通过id删除学生信息
如果存在,则删除,如果不存在,则提示删除失败。 - 要求4:删除完毕之后,遍历所有学生信息。
- 要求5:查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁
package test;
public class Student {
private int id;
private String name;
private int age;
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;
}
}
package test;
public class Test {
public static void main(String[] args) {
Student[] a = new Student[3];
Student stu1 = new Student(1,"zhangsan",23);
Student stu2 = new Student(2,"lisi",24);
a[0] = stu1;
a[1] = stu2;
Student stu4 = new Student(3,"linda",25);
boolean flag = contains(a,stu4.getId());
if(flag){
System.out.println("当前id值重复,请修改后重试!");
}else{
int count = getCount(a);
if (count == a.length) {
Student[] newarr = creatNewarr(a);
newarr[count] = stu4;
printArr(newarr);
}else{
a[count] = stu4;
printArr(a);
}
}
}
public static boolean contains(Student[] a,int id){
for (int i = 0; i < a.length; i++) {
Student stu = a[i];
if(stu != null){
int sid = stu.getId();
if (sid == id) {
return true;
}
}
}
return false;
}
public static int getCount(Student[] a){
int count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] != null) {
count++;
}
}
return count;
}
public static Student[] creatNewarr(Student[] a){
Student[] newarr = new Student[a.length+1];
for (int i = 0; i < a.length; i++) {
newarr[i] = a[i];
}
return newarr;
}
public static void printArr(Student[] a){
for (int i = 0; i < a.length; i++) {
Student stu = a[i];
if(stu != null){
System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
}
}
}
}
package test;
public class Test1 {
public static void main(String[] args) {
Student[] b =new Student[3];
Student stu1 = new Student(1, "zhangsan", 23);
Student stu2 = new Student(2, "lisi", 24);
Student stu3 = new Student(3, "wangwu", 25);
b[0] = stu1;
b[1] = stu2;
b[2] = stu3;
int index = getId(b,5);
if( index >= 0){
b[index] = null;
}else {
System.out.println("所删除的id不存在,删除失败!");
}
}
public static void printArr(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 int getId(Student[] b,int id){
for (int i = 0; i < b.length; i++) {
Student stu = b[i];
if (stu != null) {
int sid = stu.getId();
if (sid == id) {
return i;
}
}
}
return -1;
}
}
package test;
public class Test2 {
public static void main(String[] args) {
Student[] arr = new Student[3];
Student stu1 = new Student(1, "zhangsan", 23);
Student stu2 = new Student(2, "lisi", 24);
Student stu3 = new Student(3, "wangwu", 25);
arr[0] = stu1;
arr[1] = stu2;
arr[2] = stu3;
int index = getIndex(arr, 5);
if(index >= 0){
//存在, 则将他的年龄+1岁
Student stu = arr[index];
//把原来的年龄拿出来
int newAge = stu.getAge() + 1;
//把+1之后的年龄塞回去
stu.setAge(newAge);
//遍历数组
printArr(arr);
}else{
//不存在,则直接提示
System.out.println("当前id不存在,修改失败!");
}
}
public static int getIndex(Student[] arr , int id){
for (int i = 0; i < arr.length; i++) {
//依次得到每一个学生对象
Student stu = arr[i];
//对stu进行一个非空判断
if(stu != null){
int sid = stu.getId();
if(sid == id){
return i;
}
}
}
//当循环结束之后,还没有找到就表示不存在
return -1;
}
public static void printArr(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());
}
}
}
}
今天先记录到这里了!