有两个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;请教大家到底应该怎么样写呢?谢谢各位啦。
解决方案 »
- 初学java,给点建议
- SWT的GUI为什么读多线程时会卡死(使用了log4j,需加配置文件)
- 怎么应用绘图工具啊??
- 数据自动销毁
- 八个J2EE的高级群,按层次划分,共享教程代码,期待各位朋友加入(需考试)
- 有两个向量v1,v2. 如何得到v1-v2的数据。
- 请问J2SE中有播放PCM语音的类吗?
- 200分想和大家讨论一个问题:为什么有些人会说搞数据库开发只是表面的东西,技术含量不高,真正的程序员要做底层开发。
- jbuilder7里如何在project pane里显示出某个project的所有的类的结构图,就如显示source java files的结构图一样
- 各位高手,请问JAVA实现数据库有什么好处,并且怎么学习才有较高的进展啊!不胜感激!!
- 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。