我有一個String,double和float類型的數組,我需要能夠在其中搜索字符串。我試圖做一個二進制搜索,但我發現了以下錯誤,當我運行該程序,並嘗試搜索:在字符串中搜索多維數組
java.lang.ClassCastException: java.lang.String cannot be cast to Customer
at Customer.compareTo(prog4.java:1)
at java.util.Arrays.binarySearch0(Unknown Source)
at java.util.Arrays.binarySearch(Unknown Source)
at prog4.main(prog4.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
我還沒有找到搜索以3D陣列的任何其他方法,所以任何幫助將不勝感激。
這裏是我的代碼:
case 'b':
System.out.println();
System.out.println("Please enter a customer name:");
String search = kb.nextLine(); //read the 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;
地方 - 你鑄塑'Customer'哪裏,你應該把它轉換爲'String' – Nishant 2011-03-02 17:59:58