how to search an array of objects?

D

Digging4fire

hi all,

hope someone can help.

i have created a array of student objects -

each object has a string for student name
a float for mark
and a int for module number.

i need to be able to display the details for a paticular student when
the user types in the student name. so i'm thinking i need some way of
searching the array of student objects for a particular string and then
extract all details relating to that oject? not sure how though.

tia.
 
B

Brandon McCombs

hi all,

hope someone can help.

i have created a array of student objects -

each object has a string for student name
a float for mark
and a int for module number.

i need to be able to display the details for a paticular student when
the user types in the student name. so i'm thinking i need some way of
searching the array of student objects for a particular string and then
extract all details relating to that oject? not sure how though.

tia.

well, to simply solve the problem it is a matter of using something you
learned in your first programming class.

loop through the array comparing the student name string with the text
the user enters into the textfield. After a match is found grab all data
from the array element and put them into other textfields or display the
data as text in a jlabel.

depending on how many students there are you may need to use something
other than an array so it is quicker, like a hashmap.
 
L

Lew

Brandon said:
loop through the array comparing the student name string with the text
the user enters into the textfield. After a match is found grab all data
from the array element and put them into other textfields or display the
data as text in a jlabel.

There is a utility class java.util.Arrays
depending on how many students there are you may need to use something
other than an array so it is quicker, like a hashmap.

But this is truly the better way to go, and not just for speed or for a
particular size but because it is a more elegant and stable program construct.
It is worth knowing as much as you can about the Collections framework.

Let us pretend you call your student object type Student, defined like:

package eg;
public class Student
{
private String name;
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
private int module;
public int getModule() { return module; }
public void setModule( int module ) { this.name = module; }
private float mark;
public float getMark() { return mark; }
public void setMark( float mark ) { this.name = mark; }
}

Then in your business logic class, let's call it Busy, you might use this type
in a method and associated data structure like this:

package eg.test;
import eg.Student;
public class Busy
{
...
private final Map<String, Student> enrollment =
new HashMap<String, Student> ();

public void enroll( Student student )
{
enrollment.put( student.getName(), student );
}

public Student getStudent( String name )
{
return enrollment.get( name );
}
...
}

As you see, these methods are pretty thin wrappers around the Map methods, but
this example simplifies. The purpose is to show the Map idiom, rather than to
propose one actually write such wrapper methods.

- Lew
 
J

John Ersatznom

Lew said:
public class Student
{
private String name;
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
private int module;
public int getModule() { return module; }
public void setModule( int module ) { this.name = module; }
private float mark;
public float getMark() { return mark; }
public void setMark( float mark ) { this.name = mark; }
}

Ewww. Student is a value object with no behavior and no likely
implementation changes save to add or remove fields, so it should really
just be

public class Student {
public final String name;
public int module; // Consider making this an object too
public float mark; // Consider making this an int, 1-100,
// or even an enum with A, B, C, D, F
public Student (String name, int module, float mark) {
this.name = name;
this.module = module;
this.mark = mark;
}
}

Name made final since it's used as a map key.

Of course, a student is actually likely to have marks in many classes,
which suggests

public class Student {
public final String name;
// And address, and GPA, and other stuff
// equals() and hashCode() methods
}

public class Course { whatever } // or Module or whatever

public class SchoolInfo {
private Map<String, Student> students;
private Map<Student, List<Course>> whosTakingWhat;
private Map<Course, List<Student>> whosInWhat;
private static class StudentInCourse {
public final Student student;
public final Course course;
public StudentInCourse (Student student,Course course) {
this.student = student;
this.course = course;
}
// obvious equals() and hashCode() go here
}
private Map<StudentInCourse, Integer> marks;
// whatever
}

Or perhaps even:

public class Course {
private Map<Student, Integer> marks;
public Set<Student> getTakers () { return marks.keySet(); }
public boolean contains (Student student) {
return marks.containsKey(student);
}
/**
* @throws NPE if student isn't taking this course
*/
public int getMark (Student student) {
return marks.get(student).intValue();
}
public int setMark ...
}

and SchoolInfo just has Map<String, Student> students and Map<String,
Course> courses, with Course responsible for listings its students and
SchoolInfo able to supply an inner class instance giving a Set view of
the courses a given student is taking by using the objects in "courses"
and their knowledge of their students (via "contains")...
 
D

Daniel Pitts

hi all,

hope someone can help.

i have created a array of student objects -

each object has a string for student name
a float for mark
and a int for module number.

i need to be able to display the details for a paticular student when
the user types in the student name. so i'm thinking i need some way of
searching the array of student objects for a particular string and then
extract all details relating to that oject? not sure how though.

tia.

Searching:

private Student[] students;

public Student findStudent(String name) {
/* for every Student in the array */
for (Student student: students) {
/* Does the student have the right name */
if (student.name.equals(name)) {
/* found the student */
return student;
}
}
/* didn't find the student */
return null;
}

Although, a better approach would be:

public class StudentRecords {
private Map<String, Student> studentByName
= new HashMap<String, Student>();
private Student[] students;
public StudentRecords(Student...students) {
this.students = students;
for (Student student: students) {
studentByName.put(student.name, student);
}
}

public Student findStudent(String name) {
return studentByName.get(name);
}
/* any other useful methods follow */
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top