开发者

Java array problem. Cant pass values

Wen I have this code:

    import java.util.Scanner;
    import java.util.Arrays;
    public class Ex02 {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int [] matrixSize = new int [4];
            System.out.println("Insert the values matrix (matrixA_lines,MatrixA_Rows,matrixB_lines,MatrixB_Rows");
            matrixSize = matrixFill(4);
            System.out.println(Arrays.toString(matrixSize));
        }

        public static int[] matrixFill(int sizeOne){
            int i;
            Scanner sc = new Scanner(System.in);

            int [] matrixTemp = new int [sizeOne];
            for (i = 0; i<sizeOne; i++){
                matrixTemp[i] = sc.nextInt();
            }

            return matrixTemp;
        }
    }

It all works as expected. An unidimensional array is created, filled with 1,2,3,4 and them the array is print. The problem is that i want to use an bidimentsional array. I've modified the code and it gives error. Here is the modified code:

import java.util.Scanner;
import java.util.Arrays;
public class Ex02 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int [][] matrixSize = new int [1][4];
        System.out.println("Insert the values matrix (matrixA_lines,MatrixA_Rows,matrixB_lines,MatrixB_Rows");
        matrixSize[][] = matrixFill(1,4);
        System.out.println(Arrays.deepToString(matrixSize));
    }

    public static int[][] matrixFill(int sizeOne, int sizeTwo){
        int i;
        Scanner sc = new Scanner(System.in);

        int [][] matrixTemp = new int [sizeOne][sizeTwo];
        for (i = 0; i<sizeOne; i++){
            matrixTemp[0][i] = sc.nextInt();
        }

        return matrixTemp[sizeOne][开发者_Python百科sizeTwo];
    }
}

On line 21 (matrixSize[][] = matrixFill(1,4);) the error is:

cannot find symbol
symbol:   class matrixSize
location: class Ex02.Ex02

not a statement

';' expected

And on line 34(return matrixTemp[sizeOne][sizeTwo];) the error is:

incompatible types
required: int[][]
found:    int

Can someone tell me what I'm doing wrong? Just started to learn Java.

Regards,

favolas


Remove the [][] from matrixSize, and from your return value.

matrixSize = matrixFill(1,4);

And

return matrixTemp;


matrixSize[][] = matrixFill(1,4);

That's not a valid place for the [][]. The first example had it right:

matrixSize = matrixFill(1,4);


You added "[sizeOne][sizeTwo]" to the return statement in the new code; you do not need this because matrixTemp has already been declared as a 2d array. The way you changed it, you are sending one element within the array.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜