开发者

Help me with this Error in Array while Printing?

开发者 https://www.devze.com 2023-02-16 04:10 出处:网络
It\'s say\'s type mismatch.... even if an appropriate type is selected.... I am attaching the code for better understanding....

It's say's type mismatch.... even if an appropriate type is selected....

I am attaching the code for better understanding....

    public  void calucate()
    {
        int Sum=0;
        arraySumOfRows= new int[20];
        for(int i=0;i<Array1.length;i++)
        {
            for(int j=0;j<Array1.length;j++)
            {
                Sum=Sum+Array1[i][j];
                arraySumOfRows[i]=Sum;
            }

        }

        for(int i=0;i<arraySumOfRows.length;i++)
            System.out.println(Arrays.toSt开发者_高级运维ring(arraySumOfRows[i]));


    } 


Two problems:

  1. arraySumOfRows[i] is an integer and Arrays.toString() expects an array. Just use:

    System.out.println(arraySumOfRows[i]);
    
  2. Your inner loop is wrong. I should be:

    for(int j=0;j<Array1[i].length;j++)
    


Try this:

notice i moves the line arraySumOfRows[i]=Sum out of the internal (j)loop, i think it belongs to the external one (i)Loop.

public void calucate()
    {
        int Sum=0;
        arraySumOfRows= new int[20];
        for(int i=0;i<Array1.length;i++)
        {
            for(int j=0;j<Array1.length;j++)
            {
                Sum=Sum+Array1[i][j];                    
            }
            arraySumOfRows[i]=Sum;    
        }

        for(int i=0;i<arraySumOfRows.length;i++)
            System.out.println(Integer.toString(arraySumOfRows[i]));    

    } 
0

精彩评论

暂无评论...
验证码 换一张
取 消