2016-05-24 17 views
1

例如假設我有以下String處理Java中的數組(兩列)

String S = "5,a\n" + 
      "6,b\n" + 
      "9,a"; 

格式都是一樣的 - 一個數字,然後逗號,然後一個字符,然後行結束符。

對於字符串循環每一行我用

for(String a : S.split("\\n")){} 

我要學會與最高金額,當由字符組合的字符。例如,只有一個「b」,所以值是6;而「a」有兩行,所以它的值是5 + 9 = 14。因爲14是最大值,我想找出「a」和14並將它們保存在變量中。

+0

@TimBiegeleisen,你是對的。對錯字抱歉。我糾正了它 –

回答

4

你可以這樣做如下:

public static void main (String[] args) throws java.lang.Exception 
{ 
     String S = "5,a\n" + 
      "6,b\n" + 
      "9,a"; 

     String[] lines = S.split("\\n"); 
     Map<String, Integer> map = new HashMap<String, Integer>(); 
     for(String t : lines) 
     { 
      String[] e = t.split(","); 
      Integer digit = Integer.parseInt(e[0]); 
      String c = e[1]; 
      if (map.get(c) != null) 
      { 
       Integer val = map.get(c); 
       val += digit; 
       map.put(c, val); 
      } 
      else 
      { 
       map.put(c, digit); 
      } 

     } 

     int max = 0; 
     String maxKey = null; 
     for (String k : map.keySet()) 
     { 
      if (map.get(k) > max) 
      { 

       max = map.get(k); 
       maxKey = k; 
      } 
     } 

     System.out.println("The maximum key is : " + maxKey); 
     System.out.println("The maximum value is : " + max); 
    } 

輸出是:

The maximum key is : a 
The maximum value is : 14 
0

使用HashMap存儲每對,並以字母爲關鍵字。如果條目不存在,put的第一個數字。如果存在,則輸入get並添加該號碼,然後put的總和。

import java.util.HashMap; 
import java.util.Map; 

public class ParseTest { 

    public static void main(String[] args) { 
     String S = "5,a\n" + "6,b\n" + "9,a"; 

     String maxKey = null; 
     int maxVal = 0; 

     Map<String, Integer> sums = new HashMap<>(); 
     for (String a : S.split("\\n")) { 
      String[] split = a.split(","); 
      int value = Integer.parseInt(split[0]); 
      String key = split[1]; 

      if (sums.containsKey(key)) { 
       sums.put(key, sums.get(key) + value); 
      } else { 
       sums.put(key, value); 
      } 

      if (sums.get(key) > maxVal) { 
       maxVal = sums.get(key); 
       maxKey = key; 
      } 
     } 

     System.out.println("Max key: " + maxKey + ", Sum: " + maxVal); 
    } 
} 
0

完成我的回答後,我發現很多類似的答案已經貼出來了:)。反正,我的解決方案:

public static void main(String[] args) { 
    String S = "5,a\n6,b\n9,a"; 
    Map<String, Integer> map = new HashMap<String, Integer>(); 
    String highestAmountChar = ""; 
    int highestAmount = 0; 
    for (String str : S.split("\\n")) { 
     String[] amountChar = str.split(","); 
     if (map.get(amountChar[1]) == null) { 
      map.put(amountChar[1], Integer.parseInt(amountChar[0])); 
     } else { 
      map.put(amountChar[1], map.get(amountChar[1]) + Integer.parseInt(amountChar[0])); 
     } 
     if (highestAmount < map.get(amountChar[1])) { 
      highestAmount = map.get(amountChar[1]); 
      highestAmountChar = amountChar[1]; 
     } 
    } 

    System.out.println("The character " + highestAmountChar + " has highest amount " + highestAmount); 
} 
0

你可以使用這樣的事情,而不使用HashMap的或任何託收爲此事

import java.util.Arrays; 
public class Test { 
    public static void main(String args[]) { 
     String S = "5,a\n" + 
      "6,b\n" + 
      "9,a"; 
     // Separate the string by number and letter 
     String[] separated = S.split("\\n"); 
     // Create a new array to store the letters only 
     char[] letters = new char[separated.length]; 
     // Write the letter 
     for (int i = 0; i < letters.length; i++) { 
      letters[i] = separated[i].charAt(2); 
     } 
     // Sort them haha 
     Arrays.sort(letters); 

     // And now find out which letter is repeated most 
     // Store the first letter 
     char previous = letters[0]; 
     // Make it the most repeated one for now 
     char mostRepeated = letters[0]; 
     int count = 1; 
     int maxCount = 1; 
     for (int i = 1; i < letters.length; i++) { 
      // since the array is sorted if the actual letter is the same as the previous one then keep counting 
      if (letters[i] == previous) 
       count++; 
      else { 
       if (count > maxCount) { 
        mostRepeated = letters[i - 1]; 
        maxCount = count; 
       } 
       previous = letters[i]; 
       count = 1; 
      }  
     } 
     char answer = count > maxCount ? letters[letters.length-1] : mostRepeated; 
     // Once you get the letter now just add all the numbers that goes with it 
     int sum = 0; 
     for (String s:separated) { 
      if (s.charAt(2) == answer) { 
       sum += Character.getNumericValue(s.charAt(0)); 
      } 
     } 
     // Print the result by printing the letter and it sum 
    } 
}