How would I make a board using the 3d generic array that contains col, row, and the stack (contains 4, 3, 2, 1).
This is what I declared:
private int row, col, stack;
int[][][] array3Dboard = new int[row][col][stack];
I just having trouble how to make an board using the multidimensional.
public void initalized(int arg1){
this.playerID = arg1;
this.array3Dboard = new int[4][4][4];
//create a data structure for the current contents of the board and stacks.
}
Thanks.
You need to make sure the class with method initialized() has int[][][] array3Dboard declared as an instance variable.
You also need to write:
int row = 4;
int col = 4;
int stack = 4;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
for (int k = 0; k < stack; k++) {
array3Dboard[i][j][k] = 0; //replace 0 with some numerical value
}
}
}
Related
I have an assignment in which I have to create the following classes:
public class Square3x3 {
private int[][] mat = new int [3][3];
public boolean allThere(){
int[] options = {1,2,3,4,5,6,7,8,9};
for (int i = 0 ; i < NUM_OF_ROWS; i++){
for(int j =0 ; j < NUM_OF_COLS; j++){
for (int k = 0; k < options.length; k++){
if(mat[i][j] == options[k]) {
options[k] = -1;
break;
}
}
}
}
for (int num : options) {
if(num != -1) return false;
}
return true;
}
a class that represents a 3x3 2d array, and a sudoku class that is constructed by a 3x3 2d array of Square3x3 objects:
public class Sudoku {
private Square3x3[][] grid9x9 = new Square3x3[3][3];
}
I need to check if the 9x9 grid is valid, I have a method to check if a single 3x3 object is valid, but I also need to check if the entire row/col of the 9x9 grid is valid (has all the numbers from 1-9)
Here's one way to improve your data definitions using the same two classes. Define one 9 x 9 int array, and pass a sub-array to your allThere method by using the row and column indexes.
public class Sudoku {
private int[][] grid9x9 = new int[9][9];
}
public class Square3x3 {
public boolean allThere(int[][] grid, int row, int column) {
int[] options = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = row; i < row + 3; i++) {
for (int j = column; j < column + 3; j++) {
options = testOptions(options, grid[i][j]);
}
}
for (int num : options) {
if (num != -1)
return false;
}
return true;
}
private int[] testOptions(int[] options, int gridValue) {
for (int i = 0; i < options.length; i++) {
if (gridValue == options[i]) {
options[i] = -1;
break;
}
}
return options;
}
}
I cleaned up your allThere code to document that the break statement only breaks out of the options loop.
I have this line of code to create a zigzag array, its fairly simple and I already have the code for it. here's the summary of the question:
This method creates and returns a new two-dimensional integer array, which in Java is really just a one-dimensional array whose elements are one-dimensional arrays of type int[]. The returned array must have the correct number of rows that each have exactly cols columns. This array must contain the numbers start, start + 1, ..., start + (rows * cols - 1) in its rows in order, except that the elements in each odd-numbered row must be listed in descending order.
For example, when called with rows = 4, cols = 5 and start = 4, this method should create and return the two-dimensional array whose contents are
4 5 6 7 8
13 12 11 10 9
14 15 16 17 18
23 22 21 20 19
I've tried talking with my colleagues but they can't spot the problem too
public class P2J1
{
public static int[][] createZigZag(final int rows, final int cols, int start)
{
final int[][] array = new int[rows][cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array[i][j] = start;
start++;
}
}
return array;
}
}
/// heres the tester program
#Test public void testCreateZigZag() {
Random rng = new Random(SEED);
CRC32 check = new CRC32();
for(int i = 0; i < TRIALS; i++) {
int rows = rng.nextInt(20) + 1;
int cols = rng.nextInt(20) + 1;
int start = rng.nextInt(100);
int[][] zig = P2J1.createZigZag(rows, cols, start);
assertEquals(rows, zig.length);
for(int j = 0; j < rows; j++) {
assertEquals(cols, zig[j].length);
for(int e: zig[j]) { check.update(e); }
}
}
assertEquals(3465650385L, check.getValue());
}
Your column index always goes from 0 to cols-1, in that order. You need to alternate the order every other row.
You can do this by using variables for the start, end, and increment of the inner loop and assign those variables based on the row index being odd or even.
Something like this (untested):
public static int[][] createZigZag(final int rows, final int cols, int start) {
final int[][] array = new int[rows][cols];
for (int i = 0; i < rows; i++) {
boolean backwards = ((i & 1) == 1);
final int jStart = backwards ? cols-1 : -1;
final int jEnd = backwards ? 0 : cols;
final int jStep = backwards ? -1 : 1;
for (int j = jStart; j != jEnd; j += jStep) {
array[i][j] = start;
start++;
}
}
return array;
}
You could also just write two different inner loops, selected on the same condition. One would fill starting from 0, the other would fill starting from cols-1 and going backwards.
public static int[][] createZigZag(final int rows, final int cols, int start) {
final int[][] array = new int[rows][cols];
for (int i = 0; i < rows; i++) {
if ((i & 1) == 1) {
for (int j = cols-1; j >= 0; j--) {
array[i][j] = start;
start++;
}
} else {
for (int j = 0; j < cols; j++) {
array[i][j] = start;
start++;
}
}
}
return array;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Having trouble understanding compile error. Main should be left unchanged. I believe MakeandFillMatix method is the problem and I can't figure it out. I think it's how I created the method.
import java.util.*;
/*
*/
public class TesterProject
{
public static void main(String [] args)
{
int n = getMatrixSize();
int[][] m = makeAndFillMatrix(n);
printMatrix(m);
}
public static int getMatrixSize()
{
Scanner S = new Scanner(System.in);
System.out.println("give me a int to create the matrix");
int n = S.nextint();
return n;
}
public static void makeAndFillMatrix(int [][] r)
{
Random generator = new Random(5);
int rand = generator.nextInt(10);
for(int i = 0; i < r.length; i++)
{
for(int j = 0; j < r; j++)
{
r[i][j]= rand;
}
}
return r;
}
public static void printMatrix(int [][] matrix)
{
for(int r = 0; r < matrix.length; r++)
{
for(int c = 0; c < matrix[r].length; c++)
{
System.out.print(matrix[r][c] + " ");
}
System.out.println();
}
}
}
Hint
You have many problems in your code :
int n = S.nextint(); it should be int n = S.nextInt(); with Upper I
You can't compare an int with an array for (int j = 0; j < r; j++) { i think you need this for (int j = 0; j < i; j++) {
void makeAndFillMatrix(int[][] r) it not return any thing, and your return an array in the end.
makeAndFillMatrix(int[][] r) take an array of 2D and not an int int[][] m = makeAndFillMatrix(n);
Fix this problem and your problem is solve :)
EDIT
Then you have to change your code to be like this :
public static void main(String[] args) {
int n = getMatrixSize();
int[][] m = makeAndFillMatrix(n);//<<<----Problem 4
printMatrix(m);
}
public static int getMatrixSize() {
Scanner S = new Scanner(System.in);
System.out.println("give me a int to create the matrix");
int n = S.nextInt();//<<--------------Problem 1
return n;
}
public static int[][] makeAndFillMatrix(int n) {//<<<---Problem 3
Random generator = new Random(5);
int[][] r = new int[n][n];
int rand = generator.nextInt(10);
for (int i = 0; i < r.length; i++) {
for (int j = 0; j < i; j++) {//<<<-----------Problem 2
r[i][j] = rand;
}
}
return r;
}
public static void printMatrix(int[][] matrix) {
for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix[r].length; c++) {
System.out.print(matrix[r][c] + " ");
}
System.out.println();
}
}
Change makeAndFillMatrix(int[][] r) to makeAndFillMatrix(int r)
You want the matrix size, which is an integer, so take integer instead of a multidimensional array.
And in the for loop after that, change r.length to just r
Change void to int [][] in the method declaration.
In the method add a line int[][] q; and change r[i][j] = rand to q[i][j] = rand
And change return r to return q
The error you are receiving is due to you calling
int[][] m = makeAndFillMatrix(n);
in your main method.
You set n as an int in the line above, meaning n is a number, BUT, makeAndFillMatrix(int[][]) takes in a 2d array of ints. (A 2d array is a matrix, by the way.)
If you want to make a matrix of the size entered in by the user, then instead of passing n into your makeAndFillMatrix method, you would need to pass in a matrix of size n. You can do this in two ways. Option 1 is to pass in the matrix to your makeAndFillMatrix method like so:
public static void main(String [] args)
{
int n = getMatrixSize();
int[][] m = makeAndFillMatrix(new int[n][n]); // new int matrix of size n
printMatrix(m);
}
Or (the more conventional way, in my opinion), you can give makeAndFillMatrix the size of the matrix (like you are doing), and you can have makeAndFillMatrix make the array:
public static void makeAndFillMatrix(int matrixSize)
{
int[][] r = new int[matrixSize][matrixSize];
Random generator = new Random(5);
int rand = generator.nextInt(10);
for(int i = 0; i < r.length; i++)
{
for(int j = 0; j < r[i].length; j++) //~~~I modified this line as well~~~
{
r[i][j]= rand;
}
}
return r;
}
Anyways, you can take your pick at which approach you want to take to this problem, but regardless, I noticed that your makeAndFillMatrix method had an error. I fixed it and wrote //~~~I modified this line as well~~~
Your inner for loop originally said for(int j = 0; j < r; j++), but r is a matrix. For the inner loop, you would want to increase j until it has reached the size of the current row you are on in the matrix, so I changed that line as well. (Your inner loop was correct in your printMatrix method.) Whichever approach you take for your first issue, the inner loop in your makeAndFillMatrix method will need to be modified as well.
I hope this helps!
You are trying to cast 'int' (in the main function) to an 'int[][]' (the parameter of 'makeAndFillMatrix'. Also, makeAndFillMatrix returns 'void'.
I believe you want to rewrite your function to something like this:
public static int[][] makeAndFillMatrix(int r)
{
// Create a new matrix
int[][] matrix = new int[r][r];
// Get a random value
Random generator = new Random(5);
int rand = generator.nextInt(10);
// Assign random value to each cell of the matrix
for(int i = 0; i < r; i++)
{
for(int j = 0; j < r; j++)
{
r[i][j] = rand;
}
}
// Return the matrix
return matrix;
}
I'm trying to make a 2D array that essentially acts as a room/grid - like a top-down view.
This code creates an array of dimensions row x col based on user input. I then have it initialize every cell with a "." to represent a space.
My question is, why, when I use the printRoom() method does it not print anything out at all? The double nested for-loops are running through the array, but nothing gets printed.
I know it has to do with the user input, because I tried just initializing with row = 5 and col = 5, and it printed just fine.
Thank you for your help.
Here is the Room Class:
public class Room
{
int row, col;
String[][] arr = new String[row][col];
//Room Constructor
public Room(int row, int col)
{
this.row = row;
this.col = col;
}
//Fills the Room with "."
public void fillRoom()
{
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = ".";
}
}
//Prints the entire room/grid
public void printRoom()
{
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
System.out.print(arr[i][j]);
System.out.println();
}
}
}
Here is my RoomTest class which contains main:
import java.util.*;
public class RoomTest
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int row, col;
System.out.println ("Enter room dimensions (row col)");
row = scan.nextInt();
col = scan.nextInt();
Room room1 = new Room(row,col);
room1.fillRoom();
room1.printRoom();
}
}
Here is the output when I run and enter user input:
Enter room dimensions (col row)
4 6
When you initialize your array, row and col are zero, so it is a 0x0 array.
As such, the guard condition in for (int i = 0; i < arr.length; i++) is immediately false, and so never executes.
You need to move the array initialization into the constructor:
arr = new String[row][col];
Try to add length of the 2D array here because before are initialized with zero:
int row= 4, col= 6;
String[][] arr = new String[row][col];
Or you can initialize it in Constructor like this:
String[][] arr;
//Room Constructor
public Room(int row, int col)
{
arr = new String[row][col];
this.row = row;
this.col = col;
}
And in your main method initialize the row and col :
int row= 4, col= 6;
To solve the problem create the array at the constructor of Room object:
int row, col;
String[][] arr = null
//Room Constructor
public Room(int row, int col)
{
this.row = row;
this.col = col;
arr = new String[row][col];
}
I need a method that given input 2D array {{1,2},{3,4}} and (int)row=2; (int)column = 3, will produce a concatenated 2D array {{1,2,1,2,1,2}{3,4,3,4,3,4}}.
My attempt was to use a nested for loop to expand them both horizontally and and vertically, but was unsuccessful. This is what I have so far:
int row = 2;
int column = 5;
int count = 0;
int[][] list = {{12,3},{3,4}};
int [][] renewed = new int[row*list.length][column*list[0].length];
for (int l = 0; l<list.length; l++) {
for (int k = 0; k<renewed.length; k+= list.length) {
renewed[l+k] = list[l];
}
}
System.out.println(Arrays.deepToString(renewed));
}
}
^This produces list[][] expanded vertically, for the first column
int row = 2;
int column = 4;
int[][] list = {{12,3},{3,4}};
int [][] renewed = new int[row*list.length][column*list[0].length];
for (int i = 0; i<list[0].length; i++) {
for (int j = 0; j<renewed[0].length; j+=list[0].length) {
renewed[0][j+i] = list[0][i];
}
}
System.out.println(Arrays.toString(renewed[0]));
}
^This produces list[][] expanded horizontally, for the first row;
So how can I concatenate these two methods in order to produce a method that expands BOTH horizontally and vertically?
I think the easiest way is to iterate over every position in the new array and use the remainder operator % to get the right entry of the original.
int[][] list = {{1,2},{3,4}};
int row = 2;
int column = 5;
int [][] renewed = new int[row*list.length][column*list[0].length];
for (int i = 0; i < renewed.length; i++) {
for (int j = 0; j < renewed[0].length; j++) {
renewed[i][j] = list[i % list.length][j % list[0].length];
}
}
System.out.println(Arrays.deepToString(renewed));