int cannot be converted to int []

五迷三道 提交于 2019-12-03 00:25:40

问题


new to programming here and i keep getting the error message, 'incompatible types, int cannot be converted to int []', the question is to add R1& R2 together if they are of equal lengths and if not print a message that says 'the arrays must be same length', if that matters, not sure where im going wrong, any help would be greatly appreciated

 public int[] arrayAdd(int[] R1, int[] R2)
{
    int[] sumArray= new int[R1.length];

    if( R1.length!= R2.length)
    {
        System.out.println("The arrays must be same length");
}
else
{
    for(int i=0; i< R1.length; i++)
    for (int j=0; j<R2.length; j++)

    {

        sumArray= R1[i]+ R2[j]; // Error
    }
}
    return sumArray;
}

回答1:


           sumArray[i]= R1[i]+ R2[j]; // updated line

you need to assign to an array element, but you were doing it wrong.




回答2:


not sure where im going wrong

You are attempting to assign an int to a variable whose type is int[].

That is not legal ... and it doesn't make sense.

This:

   sumArray= R1[i]+ R2[j];

should be this

   sumArray[something_or_other] = R1[i] + R2[j];

... except that you have a bunch of other errors which mean that a simple "point fix" won't be correct.

Hint: you do not need nested loops to add the elements of two arrays.

-- Steve




回答3:


Your code is broken in many several ways, at least:

  1. You declared returning an array but what is the value of it when inputs are of the wrong size? Manage such errors in better ways (stop, throw exception, return error code, etc). At least never display something at this place, this is not the place were you have to tackle the error, this is the place here you detect it, just report it to caller(s).
  2. You (tried to) created space for the returned value but how could this be if conditions for having a return value is not met?
  3. You used Java syntax to declare an array, int []sumArray should be `int sumArray[0].
  4. You can't dynamically allocate an array like this, to capture a dynamic allocation you must use a pointer, an array is not a pointer. But a pointer can be set to the memory address of an allocated array, like int *sumArray = new int[10]
  5. sumArray is an array so to set an element of it use sumArray[index] = ...

So this may be better:

public int *arrayAdd(int[] R1, int[] R2) {
    if( R1.length!= R2.length) {
        return nullptr;
    }
    int *sumArray= new int[R1.length];
    for(int i=0; i< R1.length; i++) {    
        sumArray[i] = R1[i]+ R2[i]; 
    }
    return sumArray;
}

After question editing

If you want to sum two arrays, element by element, then a single loop is sufficient...




回答4:


Actually in that line sumArray is an integer array and you are assigning it as integer only and also you haven't declared variable j. Try this-

public int[] arrayAdd(int[] R1, int[] R2)
{
    int[] sumArray= new int[R1.length];

    if( R1.length!= R2.length)
    {
        System.out.println("The arrays must be same length");
    }
    else
    {
        for(int i=0; i< R1.length; i++)

        {

            sumArray[i]= R1[i]+ R2[i]; // Error
        }
    }
    return sumArray;
}


来源:https://stackoverflow.com/questions/45901985/int-cannot-be-converted-to-int

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!