我尝试对匈牙利字符串列表进行排序,但是在Android Studio 3.1中,我得到了parseException。在netbeans中,相同的代码可以很好地工作。
“ megallok”列表包含大约300个不同的字符串,例如:
0-áskmkő
Piaristáktere
smp,autóbuszforduló
等
这是我在netbeans中的代码:
public static void main(String[] args) throws IOException,
InterruptedException, ParseException {
String hunRules = ("< 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < a,A < á,Á
< b,B < c,C < cs,Cs,CS < d,D < dz,Dz,DZ < dzs,Dzs,DZS "
+ " < e,E < é,É < f,F < g,G < gy,Gy,GY < h,H < i,I < í,Í < j,J"
+ " < k,K < l,L < ly,Ly,LY < m,M < n,N < ny,Ny,NY < o,O < ó,Ó "
+ " < ö,Ö < ő,Ő < p,P < q,Q < r,R < s,S < sz,Sz,SZ < t,T "
+ " < ty,Ty,TY < u,U < ú,Ú < ü,Ü < ű,Ű < v,V < w,W < x,X < y,Y
< z,Z < zs,Zs,ZS");
String words[] = {"0-ás km kő","Posta","Széchenyi tér","BARNEVÁL"};
List<String> strList = Arrays.asList(words);
Collections.shuffle(strList);
words = strList.toArray(new String[strList.size()]);
printStrings(words);
try {
RuleBasedCollator spCollator
= new RuleBasedCollator(hunRules);
System.out.println();
sortStrings(spCollator, words);
printStrings(words);
} catch (ParseException pe) {
System.out.println("Parse exception for rules");
}
}
public static void sortStrings(Collator collator, String[] words) {
String tmp;
for (int i = 0; i < words.length; i++) {
for (int j = i + 1; j < words.length; j++) {
// Compare elements of the words array
if (collator.compare(words[i], words[j]) > 0) {
// Swap words[i] and words[j]
tmp = words[i];
words[i] = words[j];
words[j] = tmp;
}
}
}
}
以及Android Studio版本:
public class MegallokAdapter extends
RecyclerView.Adapter<MegallokAdapter.ViewHolder> {
private Context mContext;
private List<Megallok> mList;
private List<String> mList2;
private HashSet<String> hs;
MegallokAdapter(Context context, ArrayList<Megallok> list) {
mContext=context;
mList=list;
hs = new HashSet<>();
for(Megallok m : mList) {
hs.add(m.getStop());
}
mList.clear();
mList2 = new ArrayList<>(hs);
String hunRules = ("< 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < a,A < á,Á < b,B < c,C < cs,Cs,CS < d,D < dz,Dz,DZ < dzs,Dzs,DZS "
+ " < e,E < é,É < f,F < g,G < gy,Gy,GY < h,H < i,I < í,Í < j,J"
+ " < k,K < l,L < ly,Ly,LY < m,M < n,N < ny,Ny,NY < o,O < ó,Ó "
+ " < ö,Ö < ő,Ő < p,P < q,Q < r,R < s,S < sz,Sz,SZ < t,T "
+ " < ty,Ty,TY < u,U < ú,Ú < ü,Ü < ű,Ű < v,V < w,W < x,X < y,Y < z,Z < zs,Zs,ZS");
String words[] =mList2.toArray(new String[mList2.size()]);
try {
RuleBasedCollator huCollator = new RuleBasedCollator(hunRules);
sortStrings(huCollator, words);
} catch (ParseException pe) {
Log.d("teszt",pe.getMessage());
}
mList2.clear();
mList2=Arrays.asList(words);
for(String s : mList2)
{
mList.add(new Megallok(s));
}
Collections.sort(mList, new Comparator<Megallok>() {
@Override
public int compare(final Megallok object1, final Megallok object2) {
return object1.getStop().compareTo(object2.getStop());
}
});
}
public static void sortStrings(Collator collator, String[] words) {
String tmp;
for (int i = 0; i < words.length; i++) {
for (int j = i + 1; j < words.length; j++) {
if (collator.compare(words[i], words[j]) > 0) {
tmp = words[i];
words[i] = words[j];
words[j] = tmp;
}
}
}
}
}
我几乎尝试了在互联网上找到的所有内容,但没有一个解决了这个问题。