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;
}
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;
}
}
I need to delete row and column where max value in 2d array exist,please suggest how to do it.
Here is an array in which specific row and column must be delted. I suppose here can be used aaraycopy
import java.util.Scanner;
public class Test{
public static void main (String[] args){
int maxValue=0;
int[][] multiplyTab = new int[5][10];
int row = 0;
int column=0;
for (int i = 0; i < multiplyTab.length; i++) {
for (int j = 0; j <multiplyTab[i].length ; j++) {
multiplyTab[i][j] =((i+1)*(j+1));
System.out.print(multiplyTab[i][j] + "\t");
}
System.out.println();
}
for (int i = 0; i < multiplyTab.length; i++) {
for (int j = 0; j < multiplyTab[i].length; j++) {
if (multiplyTab[i][j] > maxValue) {
maxValue = multiplyTab[i][j];
row=i;
column=j;
}
}
}
}
}
To delete row i from a 2D array like yours, first create a new 2D array that is one element shorter (4 instead of 5 in your example). Copy rows 0 through i - 1 and rows i + 1 through originalTable.length - 1 into the new array.
Edit: when maxValue is in the last row (as here if I am not mistaken) and hence the last row is to be deleted, it is of course a bit easier since there are no rows i + 1 through originalTable.length - 1 to copy.
To delete column j do similarly to every inner array of the table.
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);
}
I am trying to count the number of Space objects in a Space[][], of a particular colour. When I use this method to count the number of objects of a particular colour in a row, it works fine:
public int countRowWhite(Space[][] board, int row)//TESTED//when counting in a row, the columns go up the row stays the
//same,THIS GOES THROUGH THE ROW ADDING UP NUMBERS OF WHITES
{
int count = 0;
for(int column=0; column<board.length;column++)
{
if((board[row][column]).getColour().equals(spaceColour.White))
{
count+=1;
}
}
return count;
}
However when I try this method, to count the number of objects in a column, I get an exception:
public int countColumnWhite(Space[][] board, int column)//when counting in a row, the columns go up the row stays the
//same,THIS GOES THROUGH THE ROW ADDING UP NUMBERS OF WHITES
{
int count = 0;
for(int row =0; column<board.length;row++)
{
if((board[row][column]).getColour().equals(spaceColour.White))
{
count+=1;
}
}
return count;
}
I call both of these methods in the following test method:
public void testMethods()
{
Space[][] test = new Space[5][5];
for(int i = 0; i < test.length; i++){
for(int j = 0; j < test.length; j++){
test[i][j] = new Space(spaceColour.Null);
}
}
test[0][1].setColour(spaceColour.White);
test[0][2].setColour(spaceColour.Black);
test[2][1].setColour(spaceColour.Black);
test[2][2].setColour(spaceColour.Black);
System.out.println(countColumnWhite(test, 0));
for(int row= 0; row<test.length;row++)
{
for(int column = 0; column<test.length;column++)
{
if (test[row][column].getColour().equals(spaceColour.White))
{
System.out.println("Whites at row: " + row + " and Column: "+ column);
}
}
}
If it helps, the exception is always equal to the number of rows and columns the 2d array 'test' has
I imagine that this line:
for(int row =0; column<board.length;row++)
should be:
for(int row = 0; row < board.length; row++)
Your terminating condition was checking that column is lesser than board.length, when it should be checking that row is lesser than board.length. You keep incrementing row, but the termination condition is never true, so you end up going outside the bounds of the array.
Another thing is that your code implicitly assumes that you are working with a square matrix (i.e 2-d array same number of rows and columns). So if you have unequal rows and columns, you will run into the same issue. If your assumption is valid, then this is fine. I imagine this is some kind of game board that is supposed to be square.
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;
}