-2

Simple question here:

I usually code in C++, but for this specific purpose, I am coding in java, so I'm pretty new to it. I am not sure how to switch over my syntax for this specific statement. Thanks for any help. The specific error is in 'int[] need[P][R], int[] maxm[P][R], int[] allot[P][R]', and reads as follows:

']' expected

';' expected

Any help with this small error would be great! Thank you!

int P = 5;
int R = 3;

public void calculateNeed(int[] need[P][R], int[] maxm[P][R], int[] allot[P][R]) { }
Community
  • 1
  • 1
  • 1
    Java doesn't have compile time fixed size arrays, read [this](https://stackoverflow.com/questions/4761472/alternative-to-fixed-size-arrays-in-java) for alternatives. – Sotirios Delimanolis Mar 12 '18 at 21:40
  • 1
    Java doesn't support specifying the length of array parameters (though from what I understand, C++ ignores them anyway). Just do `(int[][] need, int[][] maxm, int[][] allot)`. – shmosel Mar 12 '18 at 21:40
  • Thank you! That works! If you would like to post that as the answer I will mark it correct so you can get reputation points @shmosel – Clint Walton Mar 12 '18 at 21:43
  • Just in case you didn't know, the down-votes are probably for **lack of research**. – Zabuzard Mar 13 '18 at 00:10

1 Answers1

0

Init 2d array - matrix and print:

package com.gmail.jackkobec.java.core;

import java.util.Arrays;
import java.util.Random;

/**
 * @Author Jack <jackkobec>
 */
public class MatrixInitialisationAndPrint {
    public static final int ROWS_COUNT = 5; // Matrix rows number
    public static final int COLUMN_COUNT = 3; // Matrix columns number

    public static void main(String[] args) {
        // Init matrix in method by random int values from up to 777
        int[][] matrix = initMatrix(ROWS_COUNT, COLUMN_COUNT);
        // Print matrix
        printMatrix(matrix);
    }

    /**
     * Init matrix by random int values with border 777
     *
     * @param rowsCount    - Matrix rows number
     * @param columnsCount - Matrix columns number
     * @return int[][]
     */
    public static int[][] initMatrix(int rowsCount, int columnsCount) {
        int[][] matrix = new int[ROWS_COUNT][COLUMN_COUNT];
        for (int r = 0; r < rowsCount; r++) {
            for (int c = 0; c < columnsCount; c++) {
                matrix[r][c] = new Random().nextInt(777);
            }
        }

        return matrix;
    }

    /**
     * Prints matrix.
     *
     * @param matrix
     */
    public static void printMatrix(int[][] matrix) {
        Arrays.stream(matrix).map(array -> Arrays.toString(array) + "\n").forEach(System.out::println);
    }
}
Jackkobec
  • 5,889
  • 34
  • 34