尝试使用 Hashmap

发布于 2024-12-28 03:12:54 字数 577 浏览 1 评论 0原文

在这种情况下,每次我运行这个 for 循环时,如何将键 [i] 增加 1,按照我目前设置的方式,所有元素仅映射到 1。我试图找出每个元素有多少次数发生。我在 list.get(i) 之后的空白处尝试了 +1,但同样只将每个元素映射到 1。谢谢。

    List<Integer> list = new ArrayList<Integer>();
    HashMap<Integer,Integer> Mode = new HashMap<Integer, Integer>();
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            list.add(arr[i][j]); 
        }
    }
    System.out.println(list);
    int count = 1;
    for(int i = 0; i < list.size(); i ++) {
        Mode.put(list.get(i), );

how would I increment the key [i] by 1 in this situation every time I run through this for loop with the way I currently have it set up all the elements only get mapped to 1. I am trying to find out how many times each number occurs. I have tried +1 in the empty spot after list.get(i) but again only maps each element to 1. thank you.

    List<Integer> list = new ArrayList<Integer>();
    HashMap<Integer,Integer> Mode = new HashMap<Integer, Integer>();
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            list.add(arr[i][j]); 
        }
    }
    System.out.println(list);
    int count = 1;
    for(int i = 0; i < list.size(); i ++) {
        Mode.put(list.get(i), );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我很OK 2025-01-04 03:12:54

您需要在此处指定一个Key

for(int i = 0; i < list.size(); i++) {
        int value=list.get(i);
        if(!Mode.containsKey(value))
            Mode.put(value,1);
        else
            Mode.put(value,Mode.get(value)+1);
}

You need to specify a Key here.

for(int i = 0; i < list.size(); i++) {
        int value=list.get(i);
        if(!Mode.containsKey(value))
            Mode.put(value,1);
        else
            Mode.put(value,Mode.get(value)+1);
}
清风夜微凉 2025-01-04 03:12:54

根据你的评论,

for(int i = 0; i < list.size(); i ++) {
   if(Mode.containsKey(list.get(i)) ){
       Integer count = Mode.get(list.get(i));

       Mode.put(list.get(i), ++count);}
   else
       Mode.put(list.get(i), 1);

According to your comment,

for(int i = 0; i < list.size(); i ++) {
   if(Mode.containsKey(list.get(i)) ){
       Integer count = Mode.get(list.get(i));

       Mode.put(list.get(i), ++count);}
   else
       Mode.put(list.get(i), 1);
爱你不解释 2025-01-04 03:12:54

如果您可以选择,您可能会发现使用类似 Multiset 来自 番石榴

Multiset<Integer> seen = HashMultiset.create();
for (int[] row : arr) {
  for (int elem : row) {
    seen.add(elem); // none of that nasty dealing with the Map
  }
}
// you can look up the count of an element with seen.count(elem)
E mostCommon = null;
int highestCount = 0;
for (Multiset.Entry<Integer> entry : seen.entrySet()) {
  if (entry.getCount() > highestCount) {
    mostCommon = entry.getElement();
    highestCount = entry.getCount();
  }
}
return mostCommon; // this is the most common element

If you have the option, you may find it easier to use something like Multiset from Guava.

Multiset<Integer> seen = HashMultiset.create();
for (int[] row : arr) {
  for (int elem : row) {
    seen.add(elem); // none of that nasty dealing with the Map
  }
}
// you can look up the count of an element with seen.count(elem)
E mostCommon = null;
int highestCount = 0;
for (Multiset.Entry<Integer> entry : seen.entrySet()) {
  if (entry.getCount() > highestCount) {
    mostCommon = entry.getElement();
    highestCount = entry.getCount();
  }
}
return mostCommon; // this is the most common element
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文