使用数组基于公共密钥添加值

时间:2021-11-08 12:22:03

I have 2 arrays String key[] , long value[] . keys can be duplicated . I need unique keys and their sum of the values in a map. Below is the code I am trying but i get Array out of bound exception

我有2个数组String key [],long value []。密钥可以重复。我需要在地图中使用唯一键及其值的总和。下面是我正在尝试的代码,但我得到Array超出绑定的异常

public HashMap < String, Long > getValueMap(String key[], long value[]) {
    Map newmap = new HashMap();
    long count = 0;
    for (int i = 0; i < value.length; i++) {
        for (int j = 0; j < value.length; j++) {
            if (key[i] == key[j]) {
                count = count + value[j];
            }
        }
        newmap.put(key[i], count);

        count = 0;
    }
    return (HashMap < String, Long > ) newmap;

}


long[] value = {};
String key[] = {};
int i = 0;
        if(!worklogs.isEmpty()){
            for(Worklog wl:worklogs){
                value[i]=wl.getValue();
                key[i]=wl.getAuthor();i++;

            }
        }

HashMap < String, Long > newhm = getValueMap(key, value);
Iterator entries = newhm.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry entry = (Map.Entry) entries.next();
    String newkey = (String) entry.getKey();
    Long newvalue = (Long) entry.getValue();
    System.out.println("Key = " + newkey + ", Value = " + newvalue);
}

The above code is modified to make it look simple. Please can someone help ?

修改上面的代码使其看起来很简单。请有人帮忙吗?

4 个解决方案

#1


1  

This may be caused because arrays value and key haven't been initialized before trying to add values to them.

这可能是因为在尝试向它们添加值之前尚未初始化数组值和键。

long[] value= new long[10];

long [] value = new long [10];

String key[]= new String[10];

String key [] = new String [10];

Why do you need to use arrays instead of Lists? Because if you need the length to ve variable I think it would be a more comfortable approach.

为什么需要使用数组而不是列表?因为如果你需要变量的长度,我认为这将是一个更舒适的方法。

#2


1  

Key[] might be shorter than value[].

键[]可能比值[]短。

#3


1  

Replace

     for(i,i<10,i++){

With

for(i, i<Value.length(); i++){

#4


1  

Here array length equals 0

这里数组长度等于0

long[] value = {};

Here you try set value and get a error because array length == 0

在这里你尝试设置值并得到一个错误,因为数组长度== 0

value[i] = wl.getValue();

Use value.length instead of magic number 10

使用value.length而不是幻数10

for(int i = 0; i < value.length; i++){  
   key[i] = wl.getAuthor();
}

#1


1  

This may be caused because arrays value and key haven't been initialized before trying to add values to them.

这可能是因为在尝试向它们添加值之前尚未初始化数组值和键。

long[] value= new long[10];

long [] value = new long [10];

String key[]= new String[10];

String key [] = new String [10];

Why do you need to use arrays instead of Lists? Because if you need the length to ve variable I think it would be a more comfortable approach.

为什么需要使用数组而不是列表?因为如果你需要变量的长度,我认为这将是一个更舒适的方法。

#2


1  

Key[] might be shorter than value[].

键[]可能比值[]短。

#3


1  

Replace

     for(i,i<10,i++){

With

for(i, i<Value.length(); i++){

#4


1  

Here array length equals 0

这里数组长度等于0

long[] value = {};

Here you try set value and get a error because array length == 0

在这里你尝试设置值并得到一个错误,因为数组长度== 0

value[i] = wl.getValue();

Use value.length instead of magic number 10

使用value.length而不是幻数10

for(int i = 0; i < value.length; i++){  
   key[i] = wl.getAuthor();
}