Sile Cetoy
Sile Cetoy

Reputation: 37

Java: count occurrences in 2d array and output result in 1d array

I'm start to learn Java programming language and try to solve given task

I'm try to finish method to count occurrences in 2d array and output result in 1d

public static int[] histogram(int[][] a, int high) {
    // Please write your code after this line
    // init new array 
    int numOfRows = a.length;
    int numOfCols = a[0].length;
    int[] retVal = {};
    //main loop
    for (int o = 0; o < high; o++){
        //System.out.println(o);
        // go trough rows 
        for(int row = 0; row < numOfRows; row++ ){
            // check for colum values
            int count = 0;
            for(int col = 0; col < numOfCols; col++ ){

                if(a[row][col] = o ){
                    count++;
                }

            }
            retVal[o] = count; // Fixed typo 

        }


    }


    return retVal;
}

this is my method

I'm using BlueJ IDE for compiling and when i compile i get error "Incompatible types " for this line

if(a[row][col] = o )

i don't get it why i get error, in my opininon a[row][col] is int type ? and o is int type too.

Thanks

Upvotes: 0

Views: 1426

Answers (2)

Rishi
Rishi

Reputation: 1183

if(a[row][col] = o )

use == operator for checking equality, so finally it should be

if(a[row][col] == o )

Upvotes: 1

Yassin Hajaj
Yassin Hajaj

Reputation: 21975

if(a[row][col] = o ) // Assignment Operator

should be

if(a[row][col] == o ) // Equality evaluator

Operators JavaDoc

Upvotes: 0

Related Questions