怎样写一个添加学生方法,才可以将学生的信息(姓名,年龄)添加至一个数组,然后遍历数组,找自己想要的学生信息。求代码
2 个解决方案
#1
public class SearchStu {
public static void main(String[] args) {
Student[] stuArr = {new Student("张三",13),new Student("李四",14),new Student("王五",15)};
System.out.print("请输入要查找的学生姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for(Student temp:stuArr){
if(temp.getName().equals(name)){
System.out.println("待查询的学生信息如下:"+temp);
return;
}
}
System.out.println(name+",该学生信息不存在!");
}
}
class Student {
private String name;
private int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ".";
}
}
#2
///首先编写一个学生类
///再写一个处理类
////运行后的结果
import java.io.Serializable;
/**
* @author Administrator<br>
*学生类<br>
*/
public class Student implements Serializable {
private String name;
private int age;
public Student() {}
public Student( String name,int age) {
this.age = age;
this.name = name;
}
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;
}
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ".";
}
}
///再写一个处理类
import java.util.ArrayList;
import java.util.List;
/**
* @author Administrator<br>
*处理类<br>
*/
public class StudentUse {
/**
* @param student 需要添加学生<br>
* @param students 已有学生数组<br>
* @return Student[] 学生数组<br>
* 讲一个学生添加到一个数组中<br>
*/
public static Student[] addStudent(Student student,Student[] students){
if (student == null && students == null) {
return new Student[]{};
}
if (student == null) {
return students;
}
if (students == null) {
return new Student[]{student};
}
Student[] endStudents = new Student[students.length+1];
for (int i = 0; i < students.length; i++) {
endStudents[i] = students[i];
}
endStudents[students.length] = student;
return endStudents;
}
/**
* @param name 需要查找学生姓名<br>
* @param students 目标学生数组<br>
* @return List<Student> 符合条件的学生集合<br>
* 按照姓名查找符合的学生<br>
*/
public static List<Student> findStudentByName(String name,Student[] students){
List<Student> list = new ArrayList<Student>();
for (Student student : students) {
if (name.equals(student.getName())) {
list.add(student);
}
}
return list;
}
/**
* @param age 学生年龄<br>
* @param students 目标学生数组<br>
* @return List<Student> 符合条件的学生集合<br>
* 按照年龄查找符合条件的学生<br>
*/
public static List<Student> findStudentByAge(int age,Student[] students){
List<Student> list = new ArrayList<Student>();
for (Student student : students) {
if (age == student.getAge()) {
list.add(student);
}
}
return list;
}
/**
* @param students 需要打印的学生集合<br>
* 将集合中的学生列印出来<br>
*/
public static void printStudent(List<Student> students){
for (Student student : students) {
System.out.println(" "+student.toString());
}
}
public static void main(String[] args) {
//初始添加一个学生
Student student = new Student("张三",18);
//Student[] students = addStudent(student, null);//初始的添加可以如此书写
//为了数据多点,可以先初始一个学生数组
Student[] students = new Student[]{
new Student("张三",17),new Student("李四",15),
new Student("王五",18),new Student("张三",16),
new Student("李四",14),new Student("王五",19)};
Student[] students2 = addStudent(student, students);
//按照姓名查询学生并列印
String name = "张三";
List<Student> namelist = findStudentByName(name, students2);
System.out.println("学生姓名为"+name+"的学如下:");
printStudent(namelist);
System.out.println();
//按照年龄查询学生并列印
int age = 18;
List<Student> agelist = findStudentByAge(age, students2);
System.out.println("学生年龄为"+age+"的学如下:");
printStudent(agelist);
}
}
////运行后的结果
学生姓名为张三的学如下:
姓名:张三,年龄:17.
姓名:张三,年龄:16.
姓名:张三,年龄:18.
学生年龄为18的学如下:
姓名:王五,年龄:18.
姓名:张三,年龄:18.
#1
public class SearchStu {
public static void main(String[] args) {
Student[] stuArr = {new Student("张三",13),new Student("李四",14),new Student("王五",15)};
System.out.print("请输入要查找的学生姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
for(Student temp:stuArr){
if(temp.getName().equals(name)){
System.out.println("待查询的学生信息如下:"+temp);
return;
}
}
System.out.println(name+",该学生信息不存在!");
}
}
class Student {
private String name;
private int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ".";
}
}
#2
///首先编写一个学生类
///再写一个处理类
////运行后的结果
import java.io.Serializable;
/**
* @author Administrator<br>
*学生类<br>
*/
public class Student implements Serializable {
private String name;
private int age;
public Student() {}
public Student( String name,int age) {
this.age = age;
this.name = name;
}
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;
}
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ".";
}
}
///再写一个处理类
import java.util.ArrayList;
import java.util.List;
/**
* @author Administrator<br>
*处理类<br>
*/
public class StudentUse {
/**
* @param student 需要添加学生<br>
* @param students 已有学生数组<br>
* @return Student[] 学生数组<br>
* 讲一个学生添加到一个数组中<br>
*/
public static Student[] addStudent(Student student,Student[] students){
if (student == null && students == null) {
return new Student[]{};
}
if (student == null) {
return students;
}
if (students == null) {
return new Student[]{student};
}
Student[] endStudents = new Student[students.length+1];
for (int i = 0; i < students.length; i++) {
endStudents[i] = students[i];
}
endStudents[students.length] = student;
return endStudents;
}
/**
* @param name 需要查找学生姓名<br>
* @param students 目标学生数组<br>
* @return List<Student> 符合条件的学生集合<br>
* 按照姓名查找符合的学生<br>
*/
public static List<Student> findStudentByName(String name,Student[] students){
List<Student> list = new ArrayList<Student>();
for (Student student : students) {
if (name.equals(student.getName())) {
list.add(student);
}
}
return list;
}
/**
* @param age 学生年龄<br>
* @param students 目标学生数组<br>
* @return List<Student> 符合条件的学生集合<br>
* 按照年龄查找符合条件的学生<br>
*/
public static List<Student> findStudentByAge(int age,Student[] students){
List<Student> list = new ArrayList<Student>();
for (Student student : students) {
if (age == student.getAge()) {
list.add(student);
}
}
return list;
}
/**
* @param students 需要打印的学生集合<br>
* 将集合中的学生列印出来<br>
*/
public static void printStudent(List<Student> students){
for (Student student : students) {
System.out.println(" "+student.toString());
}
}
public static void main(String[] args) {
//初始添加一个学生
Student student = new Student("张三",18);
//Student[] students = addStudent(student, null);//初始的添加可以如此书写
//为了数据多点,可以先初始一个学生数组
Student[] students = new Student[]{
new Student("张三",17),new Student("李四",15),
new Student("王五",18),new Student("张三",16),
new Student("李四",14),new Student("王五",19)};
Student[] students2 = addStudent(student, students);
//按照姓名查询学生并列印
String name = "张三";
List<Student> namelist = findStudentByName(name, students2);
System.out.println("学生姓名为"+name+"的学如下:");
printStudent(namelist);
System.out.println();
//按照年龄查询学生并列印
int age = 18;
List<Student> agelist = findStudentByAge(age, students2);
System.out.println("学生年龄为"+age+"的学如下:");
printStudent(agelist);
}
}
////运行后的结果
学生姓名为张三的学如下:
姓名:张三,年龄:17.
姓名:张三,年龄:16.
姓名:张三,年龄:18.
学生年龄为18的学如下:
姓名:王五,年龄:18.
姓名:张三,年龄:18.