943,865 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3082
  • Java RSS
May 2nd, 2009
0

Binary search arraylist

Expand Post »
Java Syntax (Toggle Plain Text)
  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?
^
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
May 2nd, 2009
1

Re: Binary search arraylist

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.
Reputation Points: 12
Solved Threads: 3
Light Poster
Zibo is offline Offline
41 posts
since May 2009
May 2nd, 2009
0

Re: Binary search arraylist

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?
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
May 2nd, 2009
0

Re: Binary search arraylist

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:

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 12
Solved Threads: 3
Light Poster
Zibo is offline Offline
41 posts
since May 2009
May 2nd, 2009
0

Re: Binary search arraylist

Oh rite, ya i get it now, thanks for all your help.
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Files
Next Thread in Java Forum Timeline: C++ Map equivalent in Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC