2013-09-27 25 views
0

我用值填充數組列表。每一行都是具有屬性的項目。現在我想通過其中一個屬性對項目進行排序,並將它們「打印」爲textview。按任意列排序ArrayList <String[]>並「打印」結果

ArrayList<String[]> arrayList = new ArrayList<String[]>(); 
final String[] rowToArray = new String[7]; 
rowToArray[0] = itemName; 
rowToArray[1] = itemProperties1; 
rowToArray[2] = itemProperties2; 
rowToArray[3] = itemProperties3; 
rowToArray[4] = itemProperties4; 
rowToArray[5] = itemProperties5; 
rowToArray[6] = itemProperties6; 
arrayList.add(rowToArray); 

能否請你幫我按屬性進行排序,然後告訴我如何打印項目逐一與性能。

預先感謝您。

編輯:

解決的ppeterka66

我不得不添加自己的代碼,並調用Collections.sort(ArrayList中,新StringArrayComparator(列));其中列是需要排序的列。

int i=0; 
final int column=2; 
Collections.sort(arrayList,new StringArrayComparator(column)); 
for(String[] line :arrayList) 
    { 
    Log.d(Integer.toString(i),line[column].toString()); 
    } 
+1

考慮創建自己的類並將'ArrayList '更改爲'ArrayList ',那麼可以很容易地使用'Collections.sort'並創建自己的'Comparator'。 –

+0

我確認由ppeterka66製作的解決方案。此代碼正在工作,易於使用和理解!即使對我來說,所以我相信沒有其他人會有問題:)例如 – Michal

回答

1
Collections.sort  

例如

class User { 

    String name; 
    String age; 

    public User(String name, String age) { 
     this.name = name; 
     this.age = age; 
    } 

    public String getAge() { 
     return age; 
    } 

    public void setAge(String age) { 
     this.age = age; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 
import java.util.Comparator; 

public class ComparatorUser implements Comparator { 

    public int compare(Object arg0, Object arg1) { 
     User user0 = (User) arg0; 
     User user1 = (User) arg1; 

     int flag = user0.getAge().compareTo(user1.getAge()); 
     if (flag == 0) { 
      return user0.getName().compareTo(user1.getName()); 
     } else { 
      return flag; 
     } 
    } 

} 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class SortTest { 

    public static void main(String[] args) { 
     List userlist = new ArrayList(); 
     userlist.add(new User("dd", "4")); 
     userlist.add(new User("aa", "1")); 
     userlist.add(new User("ee", "5")); 
     userlist.add(new User("bb", "2")); 
     userlist.add(new User("ff", "5")); 
     userlist.add(new User("cc", "3")); 
     userlist.add(new User("gg", "6")); 

     ComparatorUser comparator = new ComparatorUser(); 
     Collections.sort(userlist, comparator); 

     for (int i = 0; i < userlist.size(); i++) { 
      User user_temp = (User) userlist.get(i); 
      System.out.println(user_temp.getAge() + "," + user_temp.getName()); 
     } 

    } 
} 
+0

。更好的學習 – sunysen

+0

我認爲這個答案是一個很好的開始,用戶正在尋找(或真正可以使用)。僅供參考,我已經提出了兩個答案 - 現在停止爭論和吃華夫餅。 :) –

1

您可以創建一個可重複使用的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... 
} 
+1

Downvoter謹慎解釋? – ppeterka

+0

好吧,這似乎是解決方案,你能幫我輸出嗎?我想看到像Log.d(「行號」,列[0] +「,」+列[1] +「,」+列[2] + ...); – Michal

+1

@Michal:我添加了一些格式化的可能性 – ppeterka