Binary search arraylist

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Binary search arraylist

 
0
  #1
May 2nd, 2009
  1. public void deleteDvd(String title)
  2. {
  3. String DvdCompare;
  4. Dvd dvdObj;
  5. Collections.sort(dvds);
  6. for(int i = 0;i< dvds.size();i++)
  7. {
  8. DvdCompare = dvds.get(i).getdvdTitle();
  9. dvdObj = dvds.get(i);
  10. if(title.equals(DvdCompare))
  11. {
  12. System.out.println("Dvd is in system");
  13. int index =Collections.binarySearch(dvds,DvdCompare);
  14.  
  15. dvds.remove(index);
  16. //delete method in arrayList
  17. }
  18. else if (!title.equals(DvdCompare))
  19. {
  20. System.out.println("Not found Dvd Object");
  21. }
  22.  
  23. }
  24.  
  25.  
  26.  
  27. }

I'm tryin to search through my dvds arraylist, find title of dvd and delete it from the arraylist completely.

I have imported
import java.util.Collections;
import java.util.ArrayList;

E:\Java\dvd>javac Inventory.java
Inventory.java:172: cannot find symbol
symbol : method sort(java.util.ArrayList<Dvd>)
location: class java.util.Collections
Collections.sort(dvds);
^
Inventory.java:180: cannot find symbol
symbol : method binarySearch(java.util.ArrayList<Dvd>,java.lang.String)
location: class java.util.Collections
int index = Collections.binarySearch(dvds,DvdC
ompare);

I get this error above and can't seem to figure it out. any ideas please?
^
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Re: Binary search arraylist

 
1
  #2
May 2nd, 2009
For first: why do You call Collections.binarySearch(...), when You already iterate through Your dvd store? If You encounter dvd, that's title equals to searched title, You already have its index ( -> i ), so binarySearch is not needed here.

For second: You'll get "Not found Dvd Object" message every iteration, when current dvd doesn't equals searched dvd.


Regarding to errors:

- "symbol : method sort(java.util.ArrayList<Dvd>)"

I think the problem is, that Your Dvd class doesn't implements Comparable interface, so that method sort, doesn't know which dvd is 'greater' and which is 'lesser'.

- Ad. binarySearch, don't know exactly how does searching by key look like, but propably again - method doesn't know what is the String key You give as argument. ( what if Dvd class has got 2 fields of String type? Which one is the given key? )
Last edited by Zibo; May 2nd, 2009 at 10:16 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Binary search arraylist

 
0
  #3
May 2nd, 2009
Thanks for the help i figured it out, just used dvds.remove(dvdObj); after all and didn't go ahead with the binarySearch method! how would i solve getting not found dvd object every iteration?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Re: Binary search arraylist

 
0
  #4
May 2nd, 2009
You need to check if dvd has been found AFTER You check Your whole dvd store, not during. So it can be for example something like that:

  1. public void deleteDvd(String title)
  2. {
  3. String DvdCompare;
  4. Dvd dvdObj;
  5. Collections.sort(dvds);
  6.  
  7. boolean found = false;
  8. for( int i=0 ;i < dvds.size(); i++)
  9. {
  10. DvdCompare = dvds.get(i).getdvdTitle();
  11. dvdObj = dvds.get(i);
  12. if(title.equals(DvdCompare))
  13. {
  14. System.out.println("Dvd is in system");
  15. dvds.remove(i);
  16.  
  17. found = true;
  18.  
  19. break;
  20. }
  21. }
  22.  
  23. if( !found )
  24. System.out.println("Dvd not found in system");
  25. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Binary search arraylist

 
0
  #5
May 2nd, 2009
Oh rite, ya i get it now, thanks for all your help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1014 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC