| | |
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: 1014 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation bank binary bluetooth chat chooser class classes client code component converter database digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image infinite input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort spamblocker sql sqlserver string superclass swing test thread threads time transfer tree windows





