-
search Vector/display one instance
Help! I have a Vector (onLoanList) that holds ResourceID's and BorrowerID's. The ResourceID's are unique but it can have multiple BorrowerID's.
I need to iterate through the list and grab the BorrowerID's, that bit's easy, but then I need to display them (still easy) but when there are multiple instances I need it to display only once. Any help greatly appreciated
-
OK this is what I understood so far:
you have a vector, tht's one object. and this vector stores another object has two properties: a borowersID and a ResourceID
java.util.vector has a method contains(Object obj) which returns true if the vector contains obj and false otherwise.
so do this
myVec; //your vector with crap inside
newVec; // a new vector
basically take what you want out of myVec and IF it isn't already in newVec add it. That way you will have a list with no iterations.
if this doesn't work b/c of how u implemented it, it's still a step in the right direction
-
thanks for your help, got it working already tho.
Did it like this
Code:
boolean found = false ;
Vector uniqueBorrowerList ;
uniqueBorrowerList = new Vector() ;
OnLoan tempOnLoan = null ;
OnLoan tempBorrower = null ;
Iterator ilist = onLoanList.iterator() ;
while (ilist.hasNext()) {
found = false ;
tempOnLoan = (OnLoan) ilist.next() ;
String id = tempOnLoan.getBorrowerID() ;
if (uniqueBorrowerList.isEmpty()) {
uniqueBorrowerList.add(tempOnLoan) ;
}
else {
Iterator blist = uniqueBorrowerList.iterator() ;
while (blist.hasNext()) {
tempBorrower = (OnLoan) blist.next() ;
if (id.equalsIgnoreCase(tempBorrower.getBorrowerID())) {
found = true;
break ;
}
}
if (!found) {
uniqueBorrowerList.add(tempOnLoan) ;
}
} // end else
} // end while ilist.hasNext()
return uniqueBorrowerList;
Might not be the best way but it works.
[ArchAngel added CODE tags]
-
it's very bad code man, I understand you're a beginner, and I dont mean to put you down but try going over it and revising it, it'll be a good exercise
ie
Code:
Vector uniqueBorrowerList ;
uniqueBorrowerList = new Vector() ;
can be reduced to :
Code:
Vector uniqueBorrowerList = new Vector() ;
[ArchAngel added CODE tags]
-
There are things about his code that I'd have picked out - basically what you were saying about the .contains(...) method.
However, I'd hardly site the code you pick out as terribly wrong. I work with people who have been programming Java for years and they prefer to separate reference variable declaration and assignment.
ArchAngel.
O:-)
-
yeah I know it should be reduced but that's the way the lecturer likes it, so gotta keep the lecturer happy, hanks for the advice tho'
-
I'm new to this forum Arch angel, I was lookin at my post and I was like "I don't remember typing that" then I saw "edited by arch angel":P
-
Using CODE tags helps break up a post and present it in an easier to read manner.
ArchAngel.
O:-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks