2016-05-10 204 views
1

我正在做一個學校項目,我需要按花費的數量(降序)打印數組。問題是,我通過將5個數組合併成一個,並且必須在for循環中收集信息來獲得空值,所以我必須在我的代碼頂部聲明數組。按列打印2d字符串數組

所以我的代碼是這樣的:

       ⇩ 

        Entry | Amount | Category  | Payment | Date 

String array[][]={ {"5", "22,2", "car payment", "visa", "21/04/2016"}, 
        {"1", "44,4", "shop", "cash", "16/02/2017"}, 
        {"2", "33,1", "shop", "cash", "15/01/2020"}, 
        {"3", "17,3", "gym", "visa", "10/01/2016"}, 
        {null, null, null, null, null}, 
        {null, null, null, null, null}, 
        {null, null, null, null, null}, 
        {null, null, null, null, null}, }; 

所以我試圖用Arrays.sort()使用Comparator,但我似乎無法得到它的權利,因爲我得到了那些空裏面我總是java.lang.NullPointerException

我想這是什麼:

Arrays.sort(array, (String[] o1, String[] o2) -> Float.compare(Float.parseFloat(o2[1]), 
         Float.parseFloat(o1[1]))); 

什麼我想要得到的是:

   {"1", "44,4", "shop", "cash", "16/02/2017"}, 
       {"2", "33,1", "shop", "cash", "15/01/2020"}, 
       {"5", "22,2", "car payment", "visa", "21/04/2016"}, 
       {"3", "17,3", "gym", "visa", "10/01/2016"}}; 

任何幫助,將如此讚賞。謝謝!

+1

我敢打賭,這個問題是在你的代碼比較。例如,如果你正在對Entry中的數組進行排序,那麼你總是可以選擇Entry null來在非null之後。 –

+0

抱歉沒有被理解。我剛剛編輯了這個問題。 –

+0

另外,你總是可以_filter_你認爲無效的字符串數組。這樣,您對於您關心的字段(在排序中)具有非空值。 –

回答

1

我相信下面的代碼工作得相當好:

public class StringArraySorter { 
    public static void main(String[] args) { 
     String array[][]={ {"5", "22,2", "car payment", "visa", "21/04/2016"}, 
       {"1", "44,4", "shop", "cash", "16/02/2017"}, 
       {"2", "33,1", "shop", "cash", "15/01/2020"}, 
       {"3", "17,3", "gym", "visa", "10/01/2016"}, 
       {null, null, null, null, null}, 
       {null, null, null, null, null}, 
       {null, null, null, null, null}, 
       {null, null, null, null, null}, 
     }; 

     Arrays.sort(array, new Comparator<String[]>() { 
      @Override 
      public int compare(String[] o1, String[] o2) { 
       String a1 = o1[1]; // amount field 
       String a2 = o2[1]; // amount field 
       if (a1 != null && a2 != null) { 
        a1 = a1.replace(',', '.'); 
        a2 = a2.replace(',', '.'); 
        return Float.compare(Float.parseFloat(a2), Float.parseFloat(a1)); 
       } 
       return 1; // non-null value for amount takes precedence, whatever that may be 
      } 
     }); 
     System.out.println(Arrays.deepToString(array)); 
    } 
} 

它打印(修改,插入新行),這是接近你的要求:

[[1, 44,4, shop, cash, 16/02/2017], 
[2, 33,1, shop, cash, 15/01/2020], 
[5, 22,2, car payment, visa, 21/04/2016], 
[3, 17,3, gym, visa, 10/01/2016], 
[null, null, null, null, null], 
[null, null, null, null, null], 
[null, null, null, null, null], 
[null, null, null, null, null]] 

現在,你總是可以修剪您的輸入數組將在將數組發送到Arrays.sort方法之前刪除具有某些關鍵字段null(或以某種其他方式無效)的記錄(字符串數組)。它使用Java 8構造(但可以說,有點神祕)將是一個初步嘗試:

Arrays.stream(array).filter(a -> a[2] != null) 
      .sorted((o1, o2) -> Float.compare(Float.parseFloat(o1[1].replace(',', '.')), Float.parseFloat(o1[1].replace(',', '.')))) 
      .forEach(a -> System.out.println("{" + Arrays.stream(a).map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")) + 
      "}")); 

,打印:

{"1", "44,4", "shop", "cash", "16/02/2017"} 
{"2", "33,1", "shop", "cash", "15/01/2020"} 
{"5", "22,2", "car payment", "visa", "21/04/2016"} 
{"3", "17,3", "gym", "visa", "10/01/2016"} 
+0

它工作smooooooth! 但是我怎樣才能打印它的格式呢?我可以使用printf還是應該爲每個使用一個? –

+0

那麼,在你的代碼中,你不檢查'null'! –

+0

是的,爲了以您想要的方式打印它,您可以遍歷數組並使用您的自定義_stringifier_。 –