Question:
Java help please(constants and multi arrays)?
DOH!
2011-07-13 14:36:54 UTC
Hi, sorry about the dumb questions but I just cant get my head around this.

Ok so I have declared a private instance variable capable of referencing a two-dimensional array of type boolean (note, not Boolean).

which is:

private boolean matrix[][];

Then Im doing a program which uses private class constants which have int values.
So for example:

private final int CONSTANT_VALUE_ONE = 5;
private final int CONSTANT_VALUE_TWO = 6;
private final int CONSTANT_VALUE_THREE = 10;

So then I created a two dimensional array of boolean that has CONSTANT_VALUE_ONE rows and (CONSTANT_VALUE_TWO times CONSTANT_VALUE_THREE) columns and assign it to the instance variable matrix.

which is:

boolean[][] matrix = new boolean [ CONSTANT_VALUE_ONE] [ CONSTANT_VALUE_TWO * CONSTANT_VALUE_THREE];

Not this is where I get stuck, I want to be able to have a method which prints a textual representation of matrix printing a '.' (or period) character for elements in the array with the value false and a 'O' (uppercase letter 'O') for elements in the array with the value true.

I know that this can be done with for loops for foreach loops and such, but I cant put pen to paper as they say!

Thank you for any help given.
Four answers:
2011-07-13 15:08:38 UTC
// The "CrMatix" class.

public class CrMatix

{

public static void main (String [] args)

{

boolean [] [] elements = new boolean[5] [60];

Matrix ob = new Matrix (elements);

ob.prMatrix ();

} // main method

} // CrMatix class



// The "Matrix" class.

public class Matrix

{

boolean mtr [] [] = null;

public Matrix (boolean mtrix [] [])

{

mtr = mtrix;

}



//do a method which takes no arguments and does not return any value,

public void prMatrix ()

{

for (int rows = 0 ; rows < mtr.length ; rows++)

{

for (int cols = 0 ; cols < mtr [0].length ; cols++)

{

if (mtr [rows] [cols])

{

System.out.print ("O");

}

else

{

System.out.print (".");

}

}

System.out.println ();

}



}

} // Matrix class
Blackcompe
2011-07-13 15:08:00 UTC
You wouldn't be assigning . and O to the array, but rather printing them as you check the array element. Yes, nested loops are appropriate for this. I can't explain it any better than that. Here's an example: http://ideone.com/A7dLq
maust
2016-10-01 06:30:22 UTC
// pay interest to the for loop and the thank you to get the value public type TwoDimNums { public static void considerable( String [] args ) { int[][] ar={{a million,2,3},{4,5,6}}; for (int i = 0; i < ar.length; i++) { for (int j = 0; j < ar[i].length; j++) { equipment.out.printf("[%d][%d] %dpercentn",i,j,ar[i][j]); } } } }
green meklar
2011-07-13 19:04:29 UTC
public static void printmatrix(boolean[][] a)

{

for(int y=0;y
{

for(int x=0;x
{

System.out.print(a[y][x]?"O":".");

}

System.out.print("\n");

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...