java - Searching multi-dimensional arrays for a String -


i have array types string, double, , float, , need able search string in it. tried binary search, i'm getting following error when run program , attempt search:

java.lang.classcastexception: java.lang.string cannot cast customer     @ customer.compareto(prog4.java:1)     @ java.util.arrays.binarysearch0(unknown source)     @ java.util.arrays.binarysearch(unknown source)     @ prog4.main(prog4.java:59)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)     @ java.lang.reflect.method.invoke(unknown source)     @ edu.rice.cs.drjava.model.compiler.javaccompiler.runcommand(javaccompiler.java:271) 

i haven't found other methods searching 3d array, appreciated.

here code:

case 'b':       system.out.println();       system.out.println("please enter customer name:");        string search = kb.nextline(); //read user's search       int place; //location of result        arrays.sort(a);        place = arrays.binarysearch(a, search);        if (place <= 0)         system.out.println("cannot find customer named " + search);      else      {        system.out.println("customer found:");          system.out.println(a[place]);      }              break; 

rather using array, want use map()

customer mycust = customers.get(search); 

another option create new customer

customer searchcust = new customer(search);  place = arrays.binarysearch(a,searchcust); 

for last section find customer correctly, you'll need implement comparable interface:

                                 // add public class customer implements comparable<customer> {      // add guy     public int compareto(customer other) {         return this.name.compareto(other.name); // i'm assuming 'name' variable of name     }  } 

or can use comparator defined in @spinttheblack's post.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -