java int 不能转换为 int []

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45901985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:58:44  来源:igfitidea点击:

int cannot be converted to int []

javaarraysbluej

提问by Melanie

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

在这里编程的新手,我不断收到错误消息,“类型不兼容,int 无法转换为 int []”,问题是如果 R1 和 R2 的长度相等,则将它们加在一起,如果不打印一条消息,说 'the数组的长度必须相同”,如果这很重要,不确定我哪里出错了,任何帮助将不胜感激

 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;
}

回答by Stephen C

not sure where im going wrong

不知道我哪里出错了

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

您正在尝试将 分配给int类型为 的变量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

——史蒂夫

回答by Rahul Raj

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

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

你需要分配给一个数组元素,但你做错了。

回答by Jean-Baptiste Yunès

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 []sumArrayshould 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. sumArrayis an array so to set an element of it use sumArray[index] = ...
  1. 您声明返回一个数组,但是当输入大小错误时它的值是多少?以更好的方式管理此类错误(停止、抛出异常、返回错误代码等)。至少永远不要在这个地方显示任何东西,这不是你必须解决错误的地方,这是你检测它的地方,只需将它报告给调用者。
  2. 您(尝试)为返回值创建了空间,但是如果不满足返回值的条件怎么办?
  3. 您使用 Java 语法声明了一个数组,int []sumArray应该是 `int sumArray[0]。
  4. 您不能像这样动态分配数组,要捕获动态分配,您必须使用指针,数组不是指针。但是可以将指针设置为已分配数组的内存地址,例如int *sumArray = new int[10]
  5. sumArray是一个数组,以便设置它的元素使用 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...

如果你想一个元素一个元素地求和两个数组,那么一个循环就足够了......

回答by Bikram Modak

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-

实际上在那行 sumArray 是一个整数数组,您仅将其分配为整数,而且您还没有声明变量 j。试试这个-

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;
}