java - How to organize multiple arrays in one class? -
this allow user book using 2 digit number. title output if input correct number. details code wherever important info written...
basically sum of program input number (refnum) matches correctly predetermined values of binarysearch array...will output book title.
class books { string refnum, title; books (string _refnum, string _title) { refnum = _refnum = enternumberfield.gettext(); //grab inputted #(continues...) (continued) compare numbers stored in array, # must equal # in binarysearch array if equal, proper title output. aka renum = 4 (also binarysearch[0]) must equal title[0] output "harry potter"
int refnum[] = new int[2]; refnum[0] = 4; refnum[1] = 6; refnum[2] = 10; int binarysearch[] = new int[2]; binarysearch[0] = 4; binarysearch[1] = 6; binarysearch[2] = 10; string title[] = new string[2]; title[0] = "harry potter"; title[1] = "chrysalids"; title[2] = "lord of flies"; } } this binary search uses inputed number find if matches anything, , if outputs corresponding title.
public static boolean binarysearch(string [ ] a, int left, int right, string v){ int middle; refnum ++; if (left > right) { return false; } middle = (left + right)/2; int compare = v.compareto(a[middle]); if (compare == 0) { return true; } if (compare < 0) { return binarysearch(a, left, middle-1, v); } else { return binarysearch(a, middle + 1, right, v); } } eventually i'll add code outputs this, first need learn how organize arrays in class , actual search, i've explained in way understand goal...so ideas?
in general should avoid several arrays in 1 class equal indexes thing. s rather done array of book:
public class book { private int refnum; private string name; public book(int refnum, string name) { this.refnum = refnum; this.name = name; } public int getrefnum() { return refnum; } public void setrefnum(int refnum) { this.refnum = refnum; } public string getname() { return name; } public void setname(string name) { this.name = name; } } // comparator allows ordering arbitrary row public refnumorder implements comparator<book> { public int compare(book b1, book b2) { return b1.getrefnum() - b2.getrefnum(); } } public class books { private list<book> books = new arraylist<book>(); public books() { books.add(new book(..); .. } public book getbyrefnum( int refnum) { collections.sort(books, new refnumorder()); int index = collections.binarysearch(book, new refnumorder()); if (index >= 0) return books.get(index); return null; } } this way can lookup row without need of handling several arrays.
Comments
Post a Comment