| | |
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
Views: 1007 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation awt binary bluetooth businessintelligence busy_handler(null) card chat class classes client code collision component constructor database draw eclipse error event eventlistener exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me jar java javafx javamicroeditionuseofmotionsensor javaprojects jmf jni jpanel jtree julia link linux list loop map method methods mobile netbeans newbie nls number object oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms socket sort sortedmaps sql string swing test textfield threads time transfer tree unlimited utility webservices windows





