search Vector/display one instance


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: search Vector/display one instance

  1. #1
    Join Date
    Nov 2003
    Posts
    3

    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

  2. #2
    Join Date
    Nov 2003
    Posts
    7
    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

  3. #3
    Join Date
    Nov 2003
    Posts
    3

    Talking

    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]

  4. #4
    Join Date
    Nov 2003
    Posts
    7
    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]

  5. #5
    Join Date
    Mar 2003
    Posts
    834
    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:-)

  6. #6
    Join Date
    Nov 2003
    Posts
    3

    Talking

    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'

  7. #7
    Join Date
    Nov 2003
    Posts
    7
    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

  8. #8
    Join Date
    Mar 2003
    Posts
    834
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center