Results 1 to 12 of 12
Thread: Search function for ArrayList?
- 04-16-2009, 12:23 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
[SOLVED] Search function for ArrayList?
Hi
I want to create a helper method that can find the index of an object example code is:
and this is the method that implements this// 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;
}
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.// 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 far I have this but it doesnt work:
When I enter a course code it doesnt show the details of the course.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
}
ThanksLast edited by javanoobie; 04-17-2009 at 08:39 PM.
- 04-16-2009, 12:31 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.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; }
- 04-16-2009, 12:35 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
Hmm i see, then how am i supposed to do it?
Last edited by javanoobie; 04-16-2009 at 12:38 AM.
- 04-16-2009, 12:44 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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?
- 04-16-2009, 01:10 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
- 04-16-2009, 01:19 AM #6
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
- 04-16-2009, 01:26 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
> 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.
- 04-16-2009, 01:30 AM #8
Can you use a map?
You could enter the course code as the key.
Mr. BeansJava Code:Map <Integer, String> myMap; myMap.put(1010, "CSC101"); myMap.get(1010); // returns "CSC101"
- 04-16-2009, 01:35 AM #9
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
^ 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.
- 04-16-2009, 01:38 AM #10
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
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
- 04-16-2009, 02:35 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 04-17-2009, 08:38 PM #12
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Code for adding search function in an application
By Avdhut in forum Threads and SynchronizationReplies: 5Last Post: 03-03-2009, 10:15 AM -
function
By nanna in forum New To JavaReplies: 1Last Post: 11-17-2008, 09:20 PM -
Need help with get function
By calicocal in forum New To JavaReplies: 10Last Post: 11-09-2008, 07:59 PM -
make search function ike eclipse search in window->preference
By i4ba1 in forum Advanced JavaReplies: 5Last Post: 08-26-2008, 03:43 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks