java学生类的数据操作

建一个student类:

package com.softeem.exercise;


public class Student {
int sno;
String sname;
String sex;
String phone;
String major;
int year;



public Student(int _sno,String _sname,String _sex,String _phone,String _major,int _year){
sno=_sno;
sname=_sname;
sex=_sex;
phone=_phone;
major=_major;
year=_year;
}
public Student(){

}
@Override
public String toString() {
return "Student [sno=" + sno + ", sname=" + sname + ", sex=" + sex + ", phone=" + phone + ", major=" + major
+ ", year=" + year + "]";
}


}

建一个学生测试类,查询所有学生

package com.softeem.exercise;


import java.util.ArrayList;


public class StudentTest {


public static void main(String[] args) {


Student[] st = new Student[6];


st[0] = new Student(1, "张三", "女", "3838", "养猪", 2018);
st[1] = new Student(2, "李四", "男", "4838", "掏粪", 2017);
st[2] = new Student(3, "王五", "女", "3838", "养鸡", 2018);
st[3] = new Student(4, "赵六", "男", "5838", "养牛", 2016);
st[4] = new Student(10, "唐七", "男", "2838", "养狗", 2015);
st[5] = new Student(12, "贺八", "女", "7838", "养鸭", 2014);


ArrayList<Student> list = new ArrayList<Student>();


// 查询出所有学生信息
for (int i = 0; i < st.length; i++) {
list.add(st[i]);
System.out.println(list.get(i).sno + " " + list.get(i).sname + " " + list.get(i).sex + " "
+ list.get(i).phone + " " + list.get(i).major + " " + list.get(i).year);
}
System.out.println("====================");
for (int i = 0; i < st.length; i++) {
list.add(st[i]); // 列出所有2018入学学生
if (list.get(i).year == 2018) {
System.out.println(list.get(i).sno + " " + list.get(i).sname);
}
// 修改学号为10学生手机号
if (list.get(i).sno == 10) {
list.get(i).phone = null;
}


}


}
}

按照学号降序输出

package com.softeem.exercise;


import java.util.ArrayList;


public class Down {
// 按照学号降序输出
public void down() {
Student[] st = new Student[6];


st[0] = new Student(1, "张三", "女", "3838", "养猪", 2018);
st[1] = new Student(2, "李四", "男", "4838", "掏粪", 2017);
st[2] = new Student(3, "王五", "女", "3838", "养鸡", 2018);
st[3] = new Student(4, "赵六", "男", "5838", "养牛", 2016);
st[4] = new Student(10, "唐七", "男", "2838", "养狗", 2015);
st[5] = new Student(12, "贺八", "女", "7838", "养鸭", 2014);


ArrayList<Student> list = new ArrayList<Student>();
for (int i = 0; i < st.length; i++) {
list.add(st[i]);
}
Student temp;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6 - 1 - i; j++) {
if (st[i].year < st[i + 1].year) {
temp = st[i];
st[i] = st[i + 1];
st[i + 1] = temp;
}
}
System.out.println(st[i].sno + " " + st[i].sname + " " + st[i].sex + " "
+ st[i].phone + " " + st[i].major + " " + st[i].year);
}



}
public static void main(String[] args) {
Down d = new Down();
d.down();
}


}

删除学号为12的学生

package com.softeem.exercise;


import java.util.ArrayList;


public class Remove {
//删除学号为12的学生
public void remove1(int sno){
Student[] st = new Student[6];


st[0] = new Student(1, "张三", "女", "3838", "养猪", 2018);
st[1] = new Student(2, "李四", "男", "4838", "掏粪", 2017);
st[2] = new Student(3, "王五", "女", "3838", "养鸡", 2018);
st[3] = new Student(4, "赵六", "男", "5838", "养牛", 2016);
st[4] = new Student(10, "唐七", "男", "2838", "养狗", 2015);
st[5] = new Student(12, "贺八", "女", "7838", "养鸭", 2014);

ArrayList<Student> list = new ArrayList<Student>();
for (int i = 0; i < st.length; i++) {
list.add(st[i]);
}



for (int i = 0; i < st.length; i++) {
if(list.get(i).sno==sno){
list.remove(i);
}
}
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).sno + " " + list.get(i).sname + " " + list.get(i).sex + " "
+ list.get(i).phone + " " + list.get(i).major + " " + list.get(i).year);
}

}


public static void main(String[] args) {
Remove s = new Remove();
s.remove1(12);

}


}

根据学号查找学生信息

package com.softeem.exercise;


import java.util.ArrayList;


public class Search {
////根据学号查找学生信息
public void Demo(int sno) {
Student[] st = new Student[6];


st[0] = new Student(1, "张三", "女", "3838", "养猪", 2018);
st[1] = new Student(2, "李四", "男", "4838", "掏粪", 2017);
st[2] = new Student(3, "王五", "女", "3838", "养鸡", 2018);
st[3] = new Student(4, "赵六", "男", "5838", "养牛", 2016);
st[4] = new Student(10, "唐七", "男", "2838", "养狗", 2015);
st[5] = new Student(12, "贺八", "女", "7838", "养鸭", 2014);


ArrayList<Student> list = new ArrayList<Student>();
for (int i = 0; i < st.length; i++) {
list.add(st[i]);
}
for (int i = 0; i < st.length; i++) {
if (list.get(i).sno == sno) {
System.out.println(list.get(i).sno + " " + list.get(i).sname + " " + list.get(i).sex + " "
+ list.get(i).phone + " " + list.get(i).major + " " + list.get(i).year);
}
}
}
public static void main(String[] args) {
Search s = new Search();
s.Demo(2);
}


}



猜你喜欢

转载自blog.csdn.net/myxqq159357/article/details/81048861