我收到以下代码的“第 9 行:错误:找不到符号”错误:已排序数组的平方 [重复]

import java.util.Arrays;

class Solution {


    public int[] sortedSquares(int[] A) {

        int[] b =new int[A.length];

        int k = 0;

        for(int i:A){

            k=i*i;

            b.add(k);  

        }

        Arrays.sort(b);

        return b;    

    }

}


翻阅古今
浏览 94回答 1
1回答

慕尼黑5688855

这不是向数组添加东西的方法,没有add数组的方法,使用索引向数组添加值for(int i =0; i<A.length; i++){&nbsp; &nbsp; k=A[i]*A[i];&nbsp; &nbsp;// you can also use Math.pow()&nbsp; &nbsp; b[i]=k;&nbsp;}使上面的clode清晰干净。通过使用 for 循环for(int i =0; i<A.length; i++){&nbsp; &nbsp; b[i]=A[i]*A[i];&nbsp; &nbsp;// you can also use Math.pow()&nbsp;}并且还通过使用每个&nbsp; &nbsp; int k = 0;&nbsp; &nbsp; for(int i:A){&nbsp; &nbsp; &nbsp; &nbsp; b[k]=i*i;&nbsp; &nbsp; &nbsp; &nbsp; k++;&nbsp;&nbsp;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java