I'm given an array (a2d) and I need to determine if every row and column has the same number of elements as every other row and column. If it is then I set the Boolean isSquare to true.
I have come up with the following code, but it doesn't like it and it isn't giving me any suggestions on how to improve it.
for(int row = 0; row < a2d.length; row++){
for(int col = 0; col < a2d[row].length; col++)
if(a2d.length == a2d[row].length)
isSquare = true;
else
isSquare = false;
}
Am I testing this the wrong way or is there a better way?
There is no need for 2 loops you should be able to do something like this (I'm not going to give the code since it's homework)
1. Save the length of the array (a2d.length)
2. Loop over all the rows
3. Check to see if the given row has the same length
4. if Not return false
5. if you reach the end of the loop return true
for (int i = 0, l = a2d.length; i < l; i++) {
if (a2d[i].length != l) {
return false;
}
}
return true;
You just need to ensure that all of the lengths of the 2nd dimension arrays are the same length as the first dimension array.
if(a2d.length == a2d[row].length)
isSquare = true;
else
isSquare = false;
If the last element passes this will return true always. Try this:
isSquare = true;
for(int row = 0; row < a2d.length; row++){
for(int col = 0; col < a2d[row].length; col++)
if(a2d.length != a2d[row].length)
isSquare = false;
}
Save the length of the array (size.length)
Loop over all the rows
Check to see if the given row has the same length
Set isSquare if true go on otherwise false, to controll the loop
if you reach the end of the loop and isSquare
return true otherwise false
private static boolean isSquare(Object[][] matrix){
boolean isSquare = true;
//Save the length of the array
int size = matrix.length;
//Loop over all the rows and Check to see if the given row has the same length
for (int i = 0; i < size && isSquare; i++) {
isSquare = size == matrix[i].length;
}
return isSquare;
}
or not puristic with fori
private static boolean isSquare(Object[][] matrix){
//Save the length of the array
int size = matrix.length;
//Loop over all the rows and Check to see if the given row has the same length
for (int i = 0; i < size; i++) {
if(size != matrix[i].length)
return false;
}
return true;
}
with enhanced for
private static boolean isSquare(Object[][] matrix){
//Save the length of the array
int size = matrix.length;
//Loop over all the rows and Check to see if the given row has the same length
for (Object[] objects : matrix) {
if (size != objects.length)
return false;
}
return true;
}
Related
I'm writing a method that traverses through a 2d array in row-major order and at the start of each row, I initialize a count variable to zero. In the inner loop, if a value is non-zero I increment the count variable. At the end of the row, if the count variable is not exactly equal to 1, return false. Ive been working on this for about 2 weeks and can't find my error. Please point me in the right direction.
** Don't mind the print statements I'm trying to see how much the count is and my code only seems to hit the second row of the array
public static boolean isGPM(int[][] matrix) {
int count =0;
for (int row = 0; row < matrix.length; row++) {
count =0;
for (int col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] > 0) {
count++;
}
else {
return !gpm;
}
}
}
System.out.println(count);
return gpm;
}
If I understand you correctly, you only care about the per-row count. This should work:
int count = 0;
// Process each row...
for (int row = 0; row < matrix.length; row++) {
count = 0;
// Process each column...
for (int col = 0; col < matrix[row].length; col++) {
// Check the cell.
if (matrix[row][col] != 0) {
count++;
}
}
// Row processing complete. Check count.
if (count != 1) {
System.out.println(count);
return gpm;
}
}
Say I have a program that creates a 4x8 board. Each cell in the board is either a colored object or an emptycell object. How do I find which row in my board is empty and nearest to row 0?
My attempt:
public int emptyRow() {
int emptyCells = 0;
int emptyRow;
for (int i = 0; i < getMaxRows(); i++){
for (int j = 0; j < getMaxCols(); j++){
if (getBoardCell(i,j).equals(BoardCell.EMPTY)){
emptyCells++;
if (emptyCells == getMaxCols()){
return i;
}
}
}
}
But I realised that this will count all the cells that are empty and I only want 8 empty cells in one row.
You will first need to create a variable for the inner for loop to count the number of items in that particular row, so you can then determine if it is empty or not. If you start at row 0, then the first row you find will be your closest row. Something like this.
int[][] myBoard = {{1,1,1},{0,0,0},{1,1,1}};
for(int i = 0; i < myBoard.length; i++) {
int count = 0;
//Loop through the columns of the row
for(int j = 0; j < myBoard[i].length; j++) {
//Check to see if the column for this row is empty if it is add
//to our empty cell count
if(myBoard[i][j] == 0) count ++;
}
//If our count is equal to the amount of columns in a row we return
//the row index.
if(count == myBoard[i].length()) return i;
}
I'm working on a game class project and got stuck.
Ive been looking everywhere on how to check if any column in a 2 dimensional Array is full and if so, the column be completely cleared.
I'm really new to Java so if you can please help me out with this!
This is the code I have so far.
//removes filled columns - added method
private boolean removeFullCol()
{
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[i].length; j++){
if(board[i][j] != occupied){
return false;
}
}
}
//empty square was never found - column is full
return true;
}
If you want to remove only full cols, check by cols, but you cant return a boolean if you want to check ALL columns. You have 2 options:
check a given column (return a boolean)
return first column full (return an int)
First option:
private boolean removeFullCol(int col)
for (int row=0; row<board[col].length; row++) {
// check if all are filled not sure which object is inside board...
if(board[row][col] == null){
return false;
}
}
return true;
}
Second option (returns first col full or data, -1 otherwise):
private int removeFullCol()
for (int row=0; row<board[col].length; row++) {
int filled = 0;
for (int row=0; row<board[col].length; row++) {
// check if all are filled not sure which object is inside board...
if(board[row][col] != null){
filled ++;
}
}
// when finished check how many rows are filled
if (board[col].length == filled)
return row;
}
return -1;
}
I wrote the code on the fly... Let me know if any errors or doubts...
Hey guys i want to make a function which checks a 2D array whether is diagonallydominant or not
Any Ideas??
I have managed to find the diagonall but how to check if diagonally dominant??
public static int arraySum(int[][] array){
int total = 0;
for (int row = 0; row < array.length; row++)
{
total += array[row][row];
}
return total;
}
According to Wikipedia, a diagonally dominant matrix is a matrix such that:
for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row.
This just checks for weak diagonal dominance, given a 2D array:
public boolean isDiagonallyDominant(int[][] array) {
int otherTotal = 0;
// Loop through every row in the array
for(int row = 0; row < array.length; row++) {
otherTotal = 0;
// Loop through every element in the row
for(int column = 0; column < array[row].length; column++) {
// If this element is NOT on the diagonal
if(column != row) {
// Add it to the running total
otherTotal += Math.abs(array[row][column]);
}
}
// If this diagonal element is LESS than the sum of the other ones...
if(Math.abs(array[row][row]) < otherTotal) {
// then the array isn't diagonally dominant and we can return.
return false;
}
}
return true;
}
In theory: in the i-th row, check that the i-th entry is smaller than the sum of the absolute values of the other values of the row:
public boolean checkDominance(int[][] matrix)
{
for (int i = 0; i < matrix.length; ++i)
{
int diagEl = Math.abs(matrix[i][i]);
int sum = 0;
for (int j = 0; j < matrix[0].lenght; ++j)
{
if (i == j) { continue; }
sum += Math.abs(matrix[i][j]);
}
if (sum > diagEl) { return (false); }
}
return (true);
}
private static boolean allNinePresent(int[][] array){
int total = 0;
for (int row = 0; **array.length**; row++){
for (int col = 0; **array[row].length**; col++){
int addEach = array[row][col];
total = addEach + total;
}
}
if (total == 45){
return true;
} else {
return false;
}
}
Shouldn't the array be an int? Why is it converting it from an int to boolean? How can I fix this.
As pointed in official tutorial for statement looks like
for (initialization; termination; increment) {
statement(s)
}
and
When the termination expression evaluates to false, the loop terminates.
In your case *array.length is used in place of termination, but in Java booleans can't be represented by integers, so something like if(1) is not valid. This means that you need to be more specific and use actual expression which can be evaluated to boolean (true or false), like
true
false
a<b
a>=b
That is why your loops should look more like
for (int row = 0; row<array.length; row++){
for (int col = 0; cor<array[row].length; col++){
BTW
if (total == 45){
return true;
} else {
return false;
}
can be rewritten to something simpler like
return total == 45;
for (int col = 0; array[row].length; col++){
second part accepts a boolean expression,
; array[row].length;
you are passing int
you need
row < array.length
and
col < array[row].length
The second part of the for loop isn't the stopping value, it's the condition under which the loop will continue looping. Use row < array.length and col < array[row].length.