java - Why isn't Collections.binarySearch outputting consistently -
so trying write program school. point of make list of cds user has in collection. program needs show list of cds both in original order (cds) , in alphabetically sorted order (cdss). information has stored in array list. have make used able add , remove programs.
when when enter song "beatles - abbey road", assumed output 1 collections.binarysearch, instead outputs -1 2 times outputs 1's. seems work other songs though.
i cannot remove song creates error
thanks help
private void buttoninitializeactionperformed(java.awt.event.actionevent evt) { //this original songs added(when initialize button pressed) collections.addall(cds, "metric - fantasies", "beatles - abbey road", "pearl jam - ten", "doors - alive", "the rolling stones - gimme shelter"); collections.addall(cdss, "metric - fantasies", "beatles - abbey road", "pearl jam - ten", "doors - alive", "the rolling stones - gimme shelter"); //once initialize button pressed other buttong able enabled buttondisplay.setenabled(true); buttonremove.setenabled(true); buttonadd.setenabled(true); buttoninitialize.setenabled(false); } private void buttondisplayactionperformed(java.awt.event.actionevent evt) { outputsonglist.settext("original order"); int condition = 0; //condition value thatis increased 1 every time each of these while loops ran. //from 1 many objects there in either cds or cdss array list //the s in cdss stands sorted while(condition < cds.size()){ outputsonglist.settext(outputsonglist.gettext() + "\n" + cds.get(condition));//writing contents of array list text area condition++; } outputsonglist.settext(outputsonglist.gettext() + "\n\n\nsorted order"); collections.sort(cdss, string.case_insensitive_order);//this sorts cdss array in alphabetical order condition = 0; while(condition < cdss.size()){ outputsonglist.settext(outputsonglist.gettext() + "\n" + cdss.get(condition));//the same previous while loop condition++; } buttondisplay.setenabled(false);//disables display button user knows information displayed } private void buttonaddactionperformed(java.awt.event.actionevent evt) { string inputsong = entersong.gettext();//getting string user typed entersong text field if(collections.binarysearch(cds, inputsong) < 0){//this checks if inputted song in arraylist cds.add(inputsong); //if is, outputted number 1...i thought cdss.add(inputsong); //if isn't numer less 0 collections.sort(cdss); //this if statement run if inputted song isn't in arrays buttondisplay.setenabled(true); } } private void buttonremoveactionperformed(java.awt.event.actionevent evt) { string inputsong = entersong.gettext(); if(collections.binarysearch(cds, inputsong) > -1){//if inputted song in array line run cds.remove(collections.binarysearch(cds, inputsong)); cdss.remove(collections.binarysearch(cds, inputsong)); buttonremove.setenabled(false); buttondisplay.setenabled(true); } }
from you've said, cds
not sorted in particular order. collections.binarysearch
states in javadoc works if list receives sorted.
instead of using collections.binarysearch
, have use cds.indexof(inputsong)
, accept linear search.
Comments
Post a Comment