Results 1 to 12 of 12
  1. #1
    javanoobie is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default [SOLVED] Search function for ArrayList?

    Hi

    I want to create a helper method that can find the index of an object example code is:

    // helper method to find the index of a specified account
    private int search(String accountNumberIn)
    {
    for(int i = 0; i < total; i++)
    {
    BankAccount tempAccount = list[i]; // find the account at index i
    String tempNumber = tempAccount.getAccountNumber(); // get account number
    if(tempNumber.equals(accountNumberIn))
    {
    return i;
    }
    }
    return -999;
    }
    and this is the method that implements this

    // return an account at a particular place in the list
    public BankAccount getItem(String accountNumberIn)
    {
    int index;
    index = search(accountNumberIn);
    if(index == -999)
    {
    return null; // indicate invalid index
    }
    else
    {
    return list[index];
    }
    }
    So basically when the user enters the account number of someone it shows up there details, rather then the user having to type in the index number. However I want this for an ArrayList and not an array.

    So far I have this but it doesnt work:

    public ArrayList<Course> getCourse(String code)
    {
    ArrayList<Course> course = new ArrayList<Course>();

    for (Course a : courses)
    {
    if (a.getCode() == code) //match found
    {
    course.add(a);
    }
    }
    return course; // result is empty if no matches are found
    }
    When I enter a course code it doesnt show the details of the course.

    Thanks
    Last edited by javanoobie; 04-17-2009 at 08:39 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    public ArrayList<Course> getCourse(String code)
    {
            // this creates a new *empty* list
        ArrayList<Course> course = new ArrayList<Course>();
            // this searches through the empty list...
        for (Course a : courses)
        {
                // ... looking for for a match with a given code
            if (a.getCode() == code) //match found
            {
                course.add(a);
            }
        }
        return course; 
    }
    I don't think you really intend to search within an empty list for a match. Also you are using == for the string comparison of code which is wrong anyway.

  3. #3
    javanoobie is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Hmm i see, then how am i supposed to do it?
    Last edited by javanoobie; 04-16-2009 at 12:38 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by javanoobie View Post
    Hmm i see, then how am i supposed to it?
    Impossible to say, given what you've posted. (Ie without the context.)

    The version using arrays refers to an array of BankAccount - list. Does the List version have access to something analogous?

    As for the use == i string comparison: you don't do this in your original code. The ArrayList version should employ the same technique of string comparison as you use in your original code.

    You might also want to say what the "ArrayList getCourse(String)" method is supposed to do: the getItem() method returned a reference to a single bank account. Is the fact that getCourse() returns a whole List of matching items intentional?

  5. #5
    javanoobie is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Impossible to say, given what you've posted. (Ie without the context.)

    The version using arrays refers to an array of BankAccount - list. Does the List version have access to something analogous?

    As for the use == i string comparison: you don't do this in your original code. The ArrayList version should employ the same technique of string comparison as you use in your original code.

    You might also want to say what the "ArrayList getCourse(String)" method is supposed to do: the getItem() method returned a reference to a single bank account. Is the fact that getCourse() returns a whole List of matching items intentional?
    basically i want the getCourse to have the same fucntionality as the getItem of the bank class, when I add a course into arraylist i input the course code and title, so what i want is a method where i can display individual course by typing in its course code.

  6. #6
    javanoobie is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Impossible to say, given what you've posted. (Ie without the context.)

    The version using arrays refers to an array of BankAccount - list. Does the List version have access to something analogous?

    As for the use == i string comparison: you don't do this in your original code. The ArrayList version should employ the same technique of string comparison as you use in your original code.

    You might also want to say what the "ArrayList getCourse(String)" method is supposed to do: the getItem() method returned a reference to a single bank account. Is the fact that getCourse() returns a whole List of matching items intentional?
    basically i want the getCourse to have the same fucntionality as the getItem of the bank class, when I add a course into arraylist i input the course code and title, so what i want is a method where i can display individual course by typing in its course code.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    > i want the getCourse to have the same fucntionality as the getItem of the bank class

    In that case getCourse() should return Course, not ArrayList<Course>.

    Implement by using a for loop (as you do now), but loop through whatever it is (you haven't said) that is a collection of all the courses. If you find a course code match, return the corresponding course. If you get right to the end of the for loop and have not found a match return null.

  8. #8
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Can you use a map?
    You could enter the course code as the key.
    Java Code:
    Map <Integer, String> myMap;
    myMap.put(1010, "CSC101");
    myMap.get(1010); // returns "CSC101"
    Mr. Beans

  9. #9
    javanoobie is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    ^ remember I already tried a key on thread about maps that I posted, but i couldnt come with a method to allocate a course to a lecturer, so i gave up on that, im trying a arraylist in the hope that it works, if it doesnt then i give up i dont know how else to make this damn program and only two weeks away from the deadline.

  10. #10
    javanoobie is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Impossible to say, given what you've posted. (Ie without the context.)

    The version using arrays refers to an array of BankAccount - list. Does the List version have access to something analogous?

    As for the use == i string comparison: you don't do this in your original code. The ArrayList version should employ the same technique of string comparison as you use in your original code.

    You might also want to say what the "ArrayList getCourse(String)" method is supposed to do: the getItem() method returned a reference to a single bank account. Is the fact that getCourse() returns a whole List of matching items intentional?
    Quote Originally Posted by pbrockway2 View Post
    > i want the getCourse to have the same fucntionality as the getItem of the bank class

    In that case getCourse() should return Course, not ArrayList<Course>.

    Implement by using a for loop (as you do now), but loop through whatever it is (you haven't said) that is a collection of all the courses. If you find a course code match, return the corresponding course. If you get right to the end of the for loop and have not found a match return null.
    I'm looping through course codes here i think.

    for example when i add a new course to the array list im inputting the course title and the course code, then to find a particular course i need to input the course code, and then its should show me the course.

    I will try out what you said.

    Thank You

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Did you get the point about there having to be something analogous to the bank account array list in the code you haven't posted?

    It's not so much how you search (a for loop is fine) but what you search. In the code you posted you search an empty ArrayList - and that's clearly no good. What you have to search is a collection of courses: it can be a List, a Map - whatever is convenient.

  12. #12
    javanoobie is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    ^ well i think ive got it to work :eek: thanks to your pointers, a vid tutorial, the bank code and a bit of improvisation!

    Thank you for your assistance

Similar Threads

  1. Code for adding search function in an application
    By Avdhut in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-03-2009, 10:15 AM
  2. function
    By nanna in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 09:20 PM
  3. Need help with get function
    By calicocal in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 07:59 PM
  4. Replies: 5
    Last Post: 08-26-2008, 03:43 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 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
  •