0

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

2
  • Can I suggest that o is a not a good choice of variable name? It looks far too much like 0 at a glance (in certain fonts). Commented Mar 15, 2016 at 20:16
  • thanks for suggestion this is anyway all not finished i'll keep your suggestion on my mind for final code. Commented Mar 15, 2016 at 20:27

2 Answers 2

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

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

if(a[row][col] == o )
Sign up to request clarification or add additional context in comments.

2 Comments

uhm. i that is it, i constantly think about operators and i miss this :(
@SileCetoy : always remember that == results in boolean
0
if(a[row][col] = o ) // Assignment Operator

should be

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

Operators JavaDoc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.