有两个class,一个用来存基本资料Studentpublic class Student
{
// the student's full name
private String name;
// the student ID
private String id;
public Student(String fullName, String studentID)
{
name = fullName;
id = studentID;
} /**
* Return the full name of this student.
*/
public String getName()
{
return name;
} /**
* Set a new name for this student.
*/
public void changeName(String replacementName)
{
name = replacementName;
} /**
* Return the student ID of this student.
*/
public String getStudentID()
{
return id;
}
/**
* Print the student's name and ID number to the output terminal.
*/
public void print()
{
System.out.println(name + " (" + id + ")");
}
}
--------------------------------FGX-------------
这一个用来分配课室import java.util.*;public class LabClass
{
private List<Student> students;
private int capacity;
/**
* Create a LabClass with a maximum number of enrolments. All other details
* are set to default values.
*/
public LabClass(int maxNumberOfStudents)
{
students = new ArrayList<Student>();
capacity = maxNumberOfStudents;
} /**
* Add a student to this LabClass.
*/
public void enrollStudent(Student newStudent)
{
if(students.size() == capacity) {
System.out.println("The class is full, you cannot enrol.");
}
else {
students.add(newStudent);
}
}
/**
* Return the number of students currently enrolled in this LabClass.
*/
public int numberOfStudents()
{
return students.size();
}
public Student findStudent(String id)
{
}
/**
* Print out a class list with other LabClass details to the standard
* terminal.
*/
public void printList()
{
System.out.println("Class list:");
for(Student student : students) {
student.print();
}
System.out.println("Number of students: " + numberOfStudents());
}
}现在问题是,要求完成查找学生id,我知道大约用if语句
public Student findStudent(String id)
{
}
但是我不懂arrayList应该怎么样调用外部class的方法,总是说代码错误
if(id == students.getStudentID());
return students;请教大家到底应该怎么样写呢?谢谢各位啦。
{
// the student's full name
private String name;
// the student ID
private String id;
public Student(String fullName, String studentID)
{
name = fullName;
id = studentID;
} /**
* Return the full name of this student.
*/
public String getName()
{
return name;
} /**
* Set a new name for this student.
*/
public void changeName(String replacementName)
{
name = replacementName;
} /**
* Return the student ID of this student.
*/
public String getStudentID()
{
return id;
}
/**
* Print the student's name and ID number to the output terminal.
*/
public void print()
{
System.out.println(name + " (" + id + ")");
}
}
--------------------------------FGX-------------
这一个用来分配课室import java.util.*;public class LabClass
{
private List<Student> students;
private int capacity;
/**
* Create a LabClass with a maximum number of enrolments. All other details
* are set to default values.
*/
public LabClass(int maxNumberOfStudents)
{
students = new ArrayList<Student>();
capacity = maxNumberOfStudents;
} /**
* Add a student to this LabClass.
*/
public void enrollStudent(Student newStudent)
{
if(students.size() == capacity) {
System.out.println("The class is full, you cannot enrol.");
}
else {
students.add(newStudent);
}
}
/**
* Return the number of students currently enrolled in this LabClass.
*/
public int numberOfStudents()
{
return students.size();
}
public Student findStudent(String id)
{
}
/**
* Print out a class list with other LabClass details to the standard
* terminal.
*/
public void printList()
{
System.out.println("Class list:");
for(Student student : students) {
student.print();
}
System.out.println("Number of students: " + numberOfStudents());
}
}现在问题是,要求完成查找学生id,我知道大约用if语句
public Student findStudent(String id)
{
}
但是我不懂arrayList应该怎么样调用外部class的方法,总是说代码错误
if(id == students.getStudentID());
return students;请教大家到底应该怎么样写呢?谢谢各位啦。
解决方案 »
- 我new了个对象然后 我把值也设到到对象里面去了 添加到list 里面去 然后list遍历出来 我怎么把遍历出来的 那个对象的属性的值取出来 呢?
- 星期英文转中文
- 确定有main方法啊啊!java.lang.NoSuchMethodError: main Exception in thread "main"
- 多版本jdk的安装与卸载
- 用Swing 设计一个注册,登录页面,不要用数据据库!急啊!
- 一个简单的JDK问题
- 用java怎么才能对网卡实现控制呢?谢谢大家~
- 散分! 求开发思路!
- 急!有关参考书,50分酬谢
- 请问,能否创建一个抽象类的实例?一定给分!!!
- Java有哪些好的练习书可以推荐一下
- JAVA实现端到端的通信
for (Student s : students) {
if (s.getId() == id) {
return s;
}
}
return null;
}
你对ArrayList的操作还不熟悉,你需要学习下
public Student findStudent(String id) {
Student result=null;
for(Student stu:students){
if(stu.getStudentID().trim().equals(id.trim())){
result=stu;
break;
}
}
return result;
}
students = new ArrayList<Student>();
你上面不是定义了嘛
for(Student s : students){
if(s.getStudentID.equals(id)){
return s;
}
}
你直接for-each迭代调用该对象的方法嘛,
而且泛型至1.5更新后自动转换的。
ioe_gaoyong
(风尘中国)
我真的对arrayList不明白啊,我是初学者。的确这个class要用数字以外的东西来作为ID。