| | |
Binary search arraylist
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 34
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
public void deleteDvd(String title) { String DvdCompare; Dvd dvdObj; Collections.sort(dvds); for(int i = 0;i< dvds.size();i++) { DvdCompare = dvds.get(i).getdvdTitle(); dvdObj = dvds.get(i); if(title.equals(DvdCompare)) { System.out.println("Dvd is in system"); int index =Collections.binarySearch(dvds,DvdCompare); dvds.remove(index); //delete method in arrayList } else if (!title.equals(DvdCompare)) { System.out.println("Not found Dvd Object"); } } }
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?
^
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? )
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.
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)
public void deleteDvd(String title) { String DvdCompare; Dvd dvdObj; Collections.sort(dvds); boolean found = false; for( int i=0 ;i < dvds.size(); i++) { DvdCompare = dvds.get(i).getdvdTitle(); dvdObj = dvds.get(i); if(title.equals(DvdCompare)) { System.out.println("Dvd is in system"); dvds.remove(i); found = true; break; } } if( !found ) System.out.println("Dvd not found in system"); }
![]() |
Similar Threads
- breadth first search tree (Java)
- Trees (Java)
- Need help with binarysearch. (Java)
- Help with Java program writing (Java)
- Help with Tertiary trees?!?! (Java)
Other Threads in the Java Forum
- Previous Thread: Files
- Next Thread: C++ Map equivalent in Java
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows





