how to search an array of objects?

Discussion in 'Java' started by Digging4fire@hotmail.com, Jan 7, 2007.

  1. Guest

    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.
    , Jan 7, 2007
    #1
    1. Advertising

  2. wrote:
    > 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.
    Brandon McCombs, Jan 7, 2007
    #2
    1. Advertising

  3. Lew Guest

    wrote:
    >> 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.


    Brandon McCombs wrote:
    > 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
    <http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html>
    that might interest you, particularly its sort() and binarySearch() methods.

    > 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
    Lew, Jan 7, 2007
    #3
  4. Lew wrote:
    > 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")...
    John Ersatznom, Jan 8, 2007
    #4
  5. Daniel Pitts Guest

    wrote:
    > 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 */
    }
    Daniel Pitts, Jan 8, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    373
    Victor Bazarov
    Mar 29, 2005
  2. John Machin
    Replies:
    2
    Views:
    277
    Reinhold Birkenfeld
    Sep 3, 2005
  3. 7stud
    Replies:
    11
    Views:
    669
    Dennis Lee Bieber
    Mar 20, 2007
  4. Abby Lee
    Replies:
    5
    Views:
    375
    Abby Lee
    Aug 2, 2004
  5. Craig Keightley
    Replies:
    10
    Views:
    255
    Craig Keightley
    Jun 29, 2005
Loading...

Share This Page