| | |
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 |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database detection draw eclipse eclipsedevelopment error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object oracle os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads time transforms tree ui web windows





