【问题标题】:Finding how many times an item Appears in an Array查找一个项目在数组中出现的次数
【发布时间】:2014-03-14 20:35:08
【问题描述】:

给定一个范围从 1 到 60 的整数数组,我试图找出数字 1-44 在数组中出现的次数。这是我的方法

public static void mostPopular(int[] list, int count)
    {
        int[] numbers;
        numbers = new int[44];
        for (int i = 0; i<count;i++)
        {
            if (list[i]<45 )
            {
                numbers[i-1]=numbers[i-1]+1; //error here
            }
        }
        for (int k=0; k<44;k++)
        {
            System.out.println("Number " + k + " occurs " + numbers[k-1]+ "times");
        }
    }

我正在尝试遍历包含 5000 多个介于 1-60 之间的数字的数组列表,然后测试该数字是否小于 45,使其成为我感兴趣的数字,然后如果整数是例如,7 会将numbers[6] 增加 1。list 是数字数组,count 是数组中有多少个总数。我不断收到 ArrayIndexOutOfBoundsException。我该如何解决这个问题?

【问题讨论】:

    标签: java arrays if-statement for-loop


    【解决方案1】:

    替换此行numbers[i-1]=numbers[i-1]+1;

    numbers[list[i] - 1] = numbers[list[i] - 1] + 1;

    现在它将更新正确元素的计数。

    【讨论】:

    • 我这样做了,它运行了,但它只是增加 numbers[0] 而不是任何其他索引
    • 你必须将他的 'numbers[list[i]]' 更改为 'numbers[list[i] - 1]' 因为在数组中索引从索引 0 开始到索引 43。
    【解决方案2】:

    你需要增加numbers[list[i]],因为你的值小于45。i上升到5000,你的数组numbers太小了。

    您应该真正开始使用调试器。所有现代 IDE 都支持它(Eclipse、IntelliJ、Netbeans 等)。使用调试器,您会很快意识到错误。

    【讨论】:

      【解决方案3】:

      如果你的初始值小于 45,它会将 numbers[i-1] 加 1。但是,由于您从 i=0 开始,它会尝试将位于 numbers[-1] 的值加 1,这在数组定律中不存在。将 i 更改为从 1 开始,您应该没问题。

      【讨论】:

        【解决方案4】:

        非常接近,但有一些索引错误,请记住 0-1 = -1,它不是可用索引。此外,这不是 c,因此您可以调用 list.length 来获取列表的大小。 试试这个(你可以忽略 mostPopular 方法之外的东西):

        class Tester{
        
           public static void main(String args[]){
               int[] list = new int[1000];
               Random random = new Random();
               for(int i=0; i<list.length; i++){
                   list[i] = random.nextInt(60) + 1;
               }
               mostPopular(list);
           }
        
           public static void mostPopular(int[] list)
           {
               int[] numbers = new int[44];
               for (int i = 0; i< list.length ;i++)
               {
                   int currentInt = list[i];
        
                   if(currentInt<45 )
                   {
                       numbers[currentInt - 1] = (numbers[currentInt -1] + 1);
                   }
               }
               for (int k=0; k<numbers.length; k++)
               {
                   System.out.println("Number " + (k+1) + " occurs " + numbers[k]+ "times");
               }
           }
        

        }

        【讨论】:

          【解决方案5】:

          i0 时,i-1-1 -- 无效索引。我认为您希望 list 中的值成为 numbers 的索引。此外,对于长度为 44 的数组,有效索引从 043。尝试一个长度为45 的数组,这样你就有了044 的有效索引。

          numbers = new int[45];
          

          if (list[i] < 45)
          {
              // Use the value of `list` as an index into `numbers`.
              numbers[list[i]] = numbers[list[i]] + 1;
          }
          

          【讨论】:

          • &amp;&amp; list 有什么作用?我将其更改为numbers[list[i]] = numbers[list[i]] + 1;,但由于某种原因,它只会增加numbers[0]
          • @user102817 我正准备使用该代码去某个地方,但决定反对它并忘记删除它。我现在已经删除了。很抱歉造成混乱。
          【解决方案6】:
          numbers[i-1]=numbers[i-1]+1; //error here 
          

          改成

          numbers[list[i]-1] += 1; 
          

          as list[i]-1 因为你的number[0] 存储了1 的频率等等。

          我们增加索引等于列表值减1的对应数组元素

          public static void mostPopular(int[] list, int count)
          {
              int[] numbers = new int[44];
              for (int i = 0; i<count;i++)
              {
                  //in case your list value has value less than 1
                  if ( (list[i]<45) && (list[i]>0) )
                  {
                      //resolve error
                      numbers[list[i]-1] += 1; 
                  }
              }
              //k should start from 1 but not 0 because we don't have index of -1
              //k < 44 change to k <= 44 because now our index is 0 to 43 with [k-1]
              for (int k=1; k <= 44;k++)
              {
                  System.out.println("Number " + k + " occurs " + numbers[k-1]+ "times");
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2012-03-04
            • 2016-05-21
            • 2023-03-19
            • 1970-01-01
            • 1970-01-01
            • 2016-12-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多