简体   繁体   中英

Iterating through the subdiagonal of a 2D Array

I am trying to iterate through a randomly generated 2d array of 0s, and 1s. In this method which I am stuck on I am trying to see if the subdiagonal has all the same numbers, all 1s, all 0s, or different numbers.

sub diagonal meaning:

110

101

011

The 0s are the subdiagonal.

this is the code I have as of now. I am trying to iterate starting at the last row and counting up to the first row diagonally.

int firstValue= matrix[matrix.length-1][0];
    int result = -1;
    for(int row = matrix.length-1; row > 0; row--)
    {
        int column = row;
        if(firstValue == matrix[row][column])
        {
            result = firstValue;
            continue;
        }
        else
        {
            result = -1;
            break;
        }
    }
    if(result== 1)
    {
        System.out.println("All " + firstValue + "s on the subdiagonal");
    }
    else if (result == 0)
    {
        System.out.println("All " + firstValue + "s on the subdiagonal");
    }
    else
    {
        System.out.println("Numbers on subdiagonal are different");
    }
}

I'm almost certain my issue is with the firstValue and/or the for loop counting up the diagonal. Any help would be appreciated, thanks much

Your issue seems to be at the following line,

for(int row = matrix.length-1; row > 0; row++) {
    ...
}

you are doing a

row = matrix.length-1; // row = array length - 1
row++ //this will increase the row's value beyond your array length

Then you will be accessing a index that does not exist causing a ArrayIndexOutOfBoundsException

Edit

what you'd want to do is,

for(int row = matrix.length-1; row >= 0; row--) {
    ....
}

This way you'd be able to iterate though your array from largest index to the smallest (0).

Edit 2

Let's say Staring array called arr has 4 elements. It'll be structured as below,

arr[0] = "test1";
arr[1] = "test2";
arr[2] = "test3";
arr[3] = "test4";

Array indexes always starts from 0, so the highest index in the above array is 3.

So if you want to iterate from smallest index to the largest, you'd do

for(int i = 0; i < arr.length; i++) {
    //i's initial value is 0 and itll increment each time the loop runs
    //loop will terminate when i is no longer < 4
    System.out.println(arr[i]);
}

and to iterate through the array in reverse order you'd do,

for(int i = (arr.length - 1); i <= 0; i--) {
    //i's initial value is (4 - 1) and it'll decrement each time the loop runs
    //loop will terminate when i smaller or equal to 0
    System.out.println(arr[i]);
}

So we want to check if all of the values in the subdiagonal are the same value, and if they are then we want to print the value that is the same. First we set aside a comparison to check the other indices

int checkValue = arr[0][arr[0].length-1];

This is the last value in the first row. Then we set a flag to catch whenever our index that we are checking matches our first value. We'll set it to false because we'll assume that the values don't match.

boolean flag = false;

Now that we have that, we need to iterate through each row in our array. We will start with the second row (arr[1]) and then we need to check the value one down and one over compared to the last value we checked (arr[1][arr.length - 1 - i]). If our first value (we assigned it's value to checkValue) and the value we are checking are the same, change the flag to true.

for (int i = 1; i < arr.length; i++)
    if (arr[i][arr.length - 1 - i] != checkValue)
        flag = true;

That'll run through all of the rows in the array. Now we have to check the state of our flag and print out the appropriate response. If the flag is true, print out that the values on the row are the same. Else we will say that the subdiagonal does not match all the way through.

if (!flag)//remember our flag is set to false, double negative equals true.
    System.out.println("All of the values on the subdiagonal are the same");
else
    System.out.println("All of the values on the subdiagonal are not the same");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM