Results 1 to 2 of 2
  1. #1
    adelgado0723 is offline Member
    Join Date
    Apr 2008
    Posts
    6
    Rep Power
    0

    Default Searching an arraylist

    The assignment requires that I write a program to update, maintain and query students' records on an ongoing basis. It requires a few things, but there's one in specific that I'm stuck on.
    This is my Student class:
    Java Code:
    public class Student {
    
        private String id,  firstname,  lastname;
    
        Student(String i, String f, String l) {
            id = i;
            firstname = f;
            lastname = l;
        }
    
        String getid() {
            return id;
        }
    
        String getfirstname() {
            return firstname;
        }
    
        String getlastname() {
            return lastname;
        }
    }
    This is my Admissions class:
    Java Code:
    import java.util.ArrayList;
    
    public class Admissions {
    
        Student s;
        ArrayList<Student> list;
        int index;
        boolean found;
    //2a
        Admissions() {
            list = new ArrayList<Student>();
        }
    //2b
        void add(Student s) {
            list.add(s);
        }
    //2c
        int size() {
            return list.size();
        }
    //2d
        boolean isempty() {
            return list.isEmpty();
        }
    //2e
        void search(String key) {
            if (list.s.getid().equalsIgnoreCase()) {
                ;
            }
        //Prof. didn't tell us how to do this. We need to figure it out, but I think it's with a while loop.
        }
    //2f 
        Student remove(int i) {
            return list.remove(i);
        }
    }
    My problem is under 2e. I need to "given an id number, locate that record on the list"
    Thanks!
    Last edited by adelgado0723; 04-15-2008 at 02:52 AM.

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Here is the loop to go through each student and check their id against the key. If this doesnt help, it should get you close enough to get the rest of your logic. Let me know.

    Java Code:
        for (Student temp : list)
        {
          if (temp.getid()==key)
          {
            found=true;
          }
        }

Similar Threads

  1. Searching XML file using DOM
    By simon in forum XML
    Replies: 5
    Last Post: 03-30-2009, 01:27 AM
  2. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  3. recursively searching through arraylists
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-17-2008, 02:36 AM
  4. searching within a JList
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 12:12 AM
  5. searching
    By nalinda in forum New To Java
    Replies: 3
    Last Post: 12-06-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •