您可以創建一個可重複使用的String [] Comparator你可以指定哪些索引到陣列比較上:
public class StringArrayComparator implements Comparator<String[]> {
//we store the index to compare the arrays by in this instance variable
private final int stringIndexToCompare;
//constructor accepting the value for the index to check
public StringArrayComparator(int whichString) {
stringIndexToCompare=whichString;
}
@Override
public int compare(String[] o1, String[] o2) {
//checking if any of the arrays is null
if(o1==null) { return o2==null?0:1; } //if o1 is null, o2 determines the resuult
else if(o2==null) { return -1; } //this only gets evaluated if o1 is not null
//get the strings, by checking if the arrays are long enough
String first = o1.length>stringIndexToCompare?o1[stringIndexToCompare]:null;
String second= o2.length>stringIndexToCompare?o2[stringIndexToCompare]:null;
//null checking the strings themselves -- basically same as above
if(first==null) { return second==null?0:1; }
else if(second==null) { return -1; }
//if both non-null, compare them.
return first.compareTo(second);
}
}
中可以使用您的清單:
Collections.sort(myList,new StringArrayComparator(3));
注意:3指定了要比較的數組的索引。
沒有指定的打印字符串應該是什麼樣子的預期輸出,只是打印的清單,你可以使用這個oneliner:
System.out.println(Arrays.deepToString(a.toArray()));
編輯
我希望看到像Log.d(「行號」,列[0] +「,」+列1 +「,」+列[2] + ...);
嘿,這看起來幾乎OK ......基本上你只要把它變成一個循環:這個是打印一行行:
int lineNo=0;
for(String[] line :myList) {
StringBuilder sb = new StringBuilder();
sb.append(++i); //line number, incrementing too
//iterating through the elements of the array
for(int col=0;col<line.lenght;col++) {
sb.append(",");
if(line[col]!=null) { //check for null....
sb.append(line[col]);
}
}
Log.d(sb.toString()); //append the value from the builder to the log.
}
爲了得到它在一個大的字符串:
int lineNo=0;
StringBuilder sb = new StringBuilder(); //create it here
for(String[] line :myList) {
sb.append(++i); //line number, incrementing too
//iterating through the elements of the array
for(int col=0;col<line.lenght;col++) {
sb.append(",");
if(line[col]!=null) { //check for null....
sb.append(line[col]);
}
}
sb.append("\n"); //append line break
}
Log.d(sb.toString()); //append the value from the builder to the log.
或者,也許它會更好(雖然較慢)使用的String.format()用於此目的,因爲這提供了更好的格式:
//assembly format string
//if no line number was needed: String format = "";
String format = "%d"; //line number, %d means integer
for(int i=0;i<7;i++) {
format+=",%20s"; //%20s means left aligned, 20 wide string
}
format += "\n"; //line break;
int lineNumber=0;
for(String[] line:myArray) {
//if you didn't need the line number, it would be so easy here
//String.format(format,line); //one line, but this doesn't have the line number yet...
//with line numbers:
int iamLazyNow = 0;
String formatted = String.format(format,++lineNumber,
line[iamLazyNow++], line[iamLazyNow++],
line[iamLazyNow++], line[iamLazyNow++],
line[iamLazyNow++], line[iamLazyNow++],
line[iamLazyNow++]); //practically one line, but ugly
//you can append formatted to a StringBuilder, or print it here...
}
考慮創建自己的類並將'ArrayList'更改爲'ArrayList ',那麼可以很容易地使用'Collections.sort'並創建自己的'Comparator'。 –
我確認由ppeterka66製作的解決方案。此代碼正在工作,易於使用和理解!即使對我來說,所以我相信沒有其他人會有問題:)例如 – Michal