2017-07-25 40 views
0

通過調用方法,在我的情況下countInNumbers將返回結果作爲數組。如何獲取並比較映射的值結果<Integer,Integer>

System.out.println(countIntInNumbers(Array)); 

結果:

{1=17, 2=10, 3=16, 4=17, 5=13, 6=22, 7=10, 8=15, 9=16, 10=19, 11=11, 12=15, 13=16, 14=13, 15=19, 16=17, 17=13, 18=21, 19=19, 20=15,} 

我嘗試數字上取決於他們的總價值不同的表分開。 示例...我想顯示其總數在3到4之間的數字,以便與其他數字分開。

面對這個問題導致結果,你可能會注意到是地圖,因爲我是新的Java,我很困惑在這一點上。

任何人都可以建議從某件事開始?

更新::: countIntInNumbers方法如下

public static Map<Integer, Integer> countIntInNumbers(int[][] mat) { 
    Map<Integer, Integer> intOccurences = new HashMap<>(); 
    for (int[] row : mat) { 
     for (int intInRow : row) { 
      Integer occurences = intOccurences.get(intInRow); 
      if (occurences == null) { // first occurrence 
       intOccurences.put(intInRow, 1); 
      } else { // increment 
       intOccurences.put(intInRow, occurences.intValue() + 1); 
      } 

     } 

    } 
    return intOccurences; 
+0

你能顯示'countIntInNumbers'的代碼? –

+1

*我想打印所有數字,其總數在3和4 *之間。一個單一的數字除了它本身之外不能有一個總數。你想說什麼?上例中的預期輸出是什麼? *我很困惑在這一點上。*我也是如此。 – shmosel

+0

發佈更新的方法; s代碼 – Annu

回答

0

我嘗試根據其總值將不同表格上的數字分開。示例...我想打印所有總數介於3和4之間的數字,以便與其他數字分開。

我們不知道你問這裏有什麼,但如果你的意思是你想顯示的有2個數字之間共有,那麼你可以不喜歡的數字:

private void printNumbers(Map<Integer, Integer> intOccurences, int minTotal, int maxTotal){ 
    boolean first = false; 
    System.out.print("{"); 
    for (Map.Entry<Integer, Integer> entry : intOccurences.entrySet()) { 
     int total = entry.getValue(); 
     if (total >= minTotal && total <= maxTotal) { 
      if (first) { 
       first = false; 
      } else { 
       System.out.print(", "); 
      } 
      System.out.print(entry.getKey() + "=" + total); 
     } 
    } 
    System.out.print("}"); 
} 

如果你是取約複製值到一個新的地圖,則可能是這樣的:

private Map<Integer, Integer> extractNumbers(Map<Integer, Integer> intOccurences, 
     int minTotal, int maxTotal) { 
    Map<Integer, Integer> result = new HashMap<>(); 
    for (Map.Entry<Integer, Integer> entry : intOccurences.entrySet()) { 
     int total = entry.getValue(); 
     if (total >= minTotal && total <= maxTotal) { 
      result.put(entry.getKey(), total); 
     } 
    } 
    // not sure if you want to remove the ones from the original map 
    return result; 
} 
+0

很高興你提到「我們不確定你在這裏問的是什麼,但如果你的意思是你想顯示的數字有一個總共在2個數字之間,那麼你可以這樣做:「 你現在可以解釋我如何將第一種方法的結果添加到剛纔創建的第一種方法的結果中以便工作嗎? – Annu

+0

什麼是「第一種方法」?你的意思是'countIntInNumbers(...)'。你需要先做這件事,然後一旦你有總數,你可以調用打印或提取方法。 – Gray

0

如果要比較的地圖的值只是按鍵得到它。然後,由於映射的值是Integer的包裝器,所以您可以使用==,> =,< =進行比較,因爲Integer equals()方法只是比較它包裝的int值與另一個Integer的int值。例如:

// Adding some test values to the map 
Map<Integer, Integer> map = new HashMap<>(); 
map.put(1, 5); 
map.put(2, 6); 
map.put(3, 5); 

// Get values by key map.get(key) 
// Compare values map.get(key) == map.get(key) or use >=, <= 
System.out.println(map.get(1) <= map.get(2)); // true 
System.out.println(map.get(1) == map.get(3)); // true 
System.out.println(map.get(1) >= map.get(2)); // false 

在你的countIntInNumbers中,你似乎只是使用它的toString()方法返回並打印地圖。如果我確定了你想要打印的值在3到4之間。在這種情況下,值是整數,所以除了整數本身之外,將不會有3和4之間的任何值。

看到您的編輯後,將原始矩陣轉換爲地圖,然後搜索您需要的值,並將它們放入新地圖中。事情是這樣的:

public static Map<Integer, Integer> countIntInNumbers(int[][] mat) { 
    Map<Integer, Integer> matConvertedToMap = new HashMap<>(); 

    for(int i=0; i<mat.length; i++) 
    { 
     matConvertedToMap.put(mat[i][0], mat[i][1]); 
    } 

    Map<Integer, Integer> intOccurences = new HashMap<>(); 

    for (Map.Entry<Integer, Integer> entry : matConvertedToMap.entrySet()) 
    { 
     if(entry.getValue() == 3 || entry.getValue() == 4) 
     { 
      intOccurences.put(entry.getKey(), entry.getValue()); 
     } 
    } 

    return intOccurences; 
} 

不知道你是預期的比較真的是什麼返回,但應該給你的如何通過地圖迭代一般的感覺。

+0

條目:map.entrySet() 類型不匹配:無法從元素類型對象轉換爲Map.Entry Annu

+0

@Annu看看我的編輯。我仍然不確定這是否是你想要做的。但正如我所說,它應該給你一個很好的提示。 – Bartzilla

+0

非常感謝你,你真的很有幫助...我有點... <3 – Annu

相關問題