Originally Posted by
innspiron
Hi,
need help, hope you will help me.
import java.util.Arrays;
import java.util.Comparator;
class LastFirstComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
int result = 0;
int i = 0;
String[] str1 = (String[]) obj1; /// this seems not working, how to change to string type?
String[] str2 = (String[]) obj2; /// because it is working with String array.
do {
result = str1[i].compareTo(str2[i]);
i++;
if (i == 7) {
break;
}
} while (result == 0);
return result;
}
}
public class LexiComparator {
public static void main(String args[]) {
Integer a1[][] = {{13, -2, 3, 99, 0, 9, 45},
{2, 3, 9, 45, 4, 44, 21},
{22, 445, 0, -5, -56, 6, 223}}; ///if I pass it as String [][] = {{"13","-2"...},{"2","3",...},{...}}; it works, but I need to use Integer type 2d array.
Arrays.sort(a1, new LastFirstComparator());
}
}
Please give some ideas. Thanks
Out put:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Integer; cannot be cast to [Ljava.lang.String;
at finalproject.LastFirstComparator.compare(LexiCompa rator.java:23)...