首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java中的数组处理(两列)

Java中的数组处理(两列)
EN

Stack Overflow用户
提问于 2016-05-24 00:51:40
回答 4查看 238关注 0票数 1

例如,假设我有以下String

代码语言:javascript
复制
String S = "5,a\n" +
           "6,b\n" +
           "9,a";

格式总是相同的-一个数字,然后逗号,然后一个字符,然后行结束字符。

用于循环字符串中的每一行

代码语言:javascript
复制
for(String a : S.split("\\n")){}

我想学习最高数量的字符,当按字符分组时。例如,只有一个"b",所以值是6;而"a“有两行,所以它的值是5+9= 14。因为这里的最大值是14,所以我想找出"a”和14,并将它们保存在变量中。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-05-24 01:09:58

您可以这样做:

代码语言:javascript
复制
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 );
    }

产出如下:

代码语言:javascript
复制
The maximum key is : a
The maximum value is : 14
票数 4
EN

Stack Overflow用户

发布于 2016-05-24 01:09:01

使用HashMap来存储每一对,并以字母作为密钥。如果条目不存在,则put第一个数字。如果存在,则get条目并添加数字,然后put求和。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-05-24 01:40:28

在完成我的回答后,我发现很多类似的答案都被张贴了出来:)。总之,我的解决方案是:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37402787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档