0

I get an out of bounds error for the line: int currentInt = matrix[i][j];

public class Matrix
{
    private int[][] matrix;

    /**
     * Constructor for objects of class Matrix
     * @param array a 2-d array
     */
    public Matrix(int[][] array)
    {
       matrix = array;
    }

    public int min()
    {
       int min = matrix[0][0];
        for(int i = 0; i < matrix.length; i++)
        {
            for(int j = 0; i < matrix[0].length; i++)
            {
                int currentInt = matrix[i][j];
                if(min > currentInt)
                {
                   min = currentInt;
                }


            }
        }
        return min;
    }

}

3 Answers 3

2
for(int j = 0; i < matrix[0].length; i++)

should be

for(int j = 0; j < matrix[i].length; j++)

or if the array is square :

for(int j = 0; j < matrix[0].length; j++)
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot to change the j in the 2nd for loop

for(int j = 0; i < matrix[0].length; i++)

should be

for(int j = 0; j < matrix[0].length; j++)

Comments

1

Change

for(int j = 0; i < matrix[0].length; i++)

to

for(int j = 0; j < matrix[i].length; j++)

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.