对象数组的增删改查
有三个源文件
Student.java
import java.util.Arrays;
import java.util.Scanner;
public class Student extends MyComparator implements Comparable<Student>
{
int id;
String name;
String sex;
int age;
double score;
Student()
{
}
public Student(int id, String name, String sex, int age ,double score) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age=age;
this.score = score;
}
@Override
public String toString() {
return "id:" + id + ", 姓名:" + name + ", 性别:" + sex + " , 年龄: " + age + ", 分数" + score;
}
public void print(Student arr[])
{
for(Student i:arr)
{
System.out.println(i);
}
}
public Student[] add(Student arr[])
{
int count =arr.length;
Scanner sc=new Scanner(System.in);
Student stu=new Student();
System.out.println("请输入学生id:");
stu.id=sc.nextInt();
System.out.println("请输入学生姓名:");
stu.name=sc.next();
System.out.println("请输入学生性别:");
stu.sex=sc.next();
System.out.println("请输入学生年龄:");
stu.age=sc.nextInt();
System.out.println("请输入学生成绩:");
stu.score=sc.nextDouble();
if(count>=arr.length)
{
Student []arr1=Arrays.copyOf(arr, (arr.length+1));
arr=arr1;
}
arr[count]=stu;
return arr;
}
public static Student[] delete(Student arr[],int id)
{
for(int i=0;i<arr.length;i++)
{
if((arr[i].id)==(id))
{
for(int j=i;j<arr.length-1;j++)
{
arr[j]=arr[j+1];
}
Student []arr1=Arrays.copyOf(arr, arr.length-1);
return arr1;
}
}
return arr;
}
public static Student[] set(Student arr[],int id)
{
Scanner sc=new Scanner(System.in);
boolean y=true;
for(int i=0;i<arr.length;i++)
{
if((arr[i].id)==(id))
{
y=false;
System.out.println("请选择需要修改的类型\n1-id\n2-姓名\n3-性别\n4-年龄\n5-成绩");
int p=sc.nextInt();
switch(p)
{
case 1:
System.out.println("请输入新学生id:");
arr[i].id=sc.nextInt();
break;
case 2:
System.out.println("请输入新学生姓名:");
arr[i].name=sc.next();
break;
case 3:
System.out.println("请输入新学生性别:");
arr[i].sex=sc.next();
break;
case 4:
System.out.println("请输入新学生年龄:");
arr[i].age=sc.nextInt();
break;
case 5:
System.out.println("请输入新学生成绩:");
arr[i].score=sc.nextDouble();
break;
}
}
}
if(y==true)
{
System.out.println("没找到该学生");
}
return arr;
}
public static void get(Student arr[],String str)
{
boolean y=true;
for(int i=0;i<arr.length;i++)
{
if(arr[i].name.equals(str)||(str.valueOf(arr[i].id).equals(str)))
{
y=false;
System.out.println(arr[i]);
}
}
if(y==true)
{
System.out.println("没找到该学生");
}
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public int compareTo(Student s)
{
if(this.id>s.id)
{
return 1;
}
if(this.id<s.id)
{
return -1;
}
return 0;
}
}
Test.java
import java.util.Arrays;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Student []arr= {
new Student(3,"小杰","男",25,99.0),
new Student(4,"小孙","女",25,90.0),
new Student(2,"老王","男",26,66.0),
new Student(1,"大毛","女",24,88.0)
};
Student stu=new Student();
while(true)
{
System.out.println(""
+ "1-打印所有学生信息"
+ "\n2-添加学生信息"
+ "\n3-删除学生信息"
+ "\n4-修改学生信息"
+ "\n5-查找学生信息"
+ "\n6-按id进行排序"
+ "\n7-按性别进行排序"
+ "\n请选择需要实现的功能");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
switch(i)
{
case 1:
stu.print(arr);
break;
case 2:
arr=stu.add(arr);
stu.print(arr);
break;
case 3:
System.out.println("请输入需要删除的学生id");
int j=sc.nextInt();
arr=Student.delete(arr, j);
stu.print(arr);
break;
case 4:
System.out.println("请输入需要修改的学生id");
int k=sc.nextInt();
arr=Student.set(arr, k);
stu.print(arr);
break;
case 5:
System.out.println("请输入需要查找学生的id或姓名");
String str=sc.next();
Student.get(arr,str);
break;
case 6:
Arrays.sort(arr);
stu.print(arr);
break;
case 7:
Arrays.sort(arr,new MyComparator());
stu.print(arr);
break;
}
System.out.println("是否继续");
String str=sc.next();
char a=str.charAt(0);
if(a=='n'||a=='N')
{
break;
}
}
}
}
MyComparator.java
import java.util.Comparator;
public class MyComparator implements Comparator<Student>
{
public int compare(Student stu1, Student stu2)
{
if(stu1.sex.hashCode()>stu2.sex.hashCode())
{
return -1;
}
if(stu1.sex.hashCode()<stu2.sex.hashCode())
{
return 1;
}
return 0;
}
}