Skip to main content

Creating 9 subarrays from a 9x9 2d array java



Hello I need help creating 9 sub arrays of 3x3 dimensions from a 9x9 array. I've seen that stackOverflow had a similar question already asked but unfortunately it was in c++. Can anyone point me in the right direction of how to create a a sub array.





Edit: had aa similar changed to had a similar







public static void Validate(final int[][] sudokuBoard)

{

int width = sudokuBoard[0].length;

int height = sudokuBoard.length;



for(int i = 0; i < width; i++)



if(!IsValidRow(sudokuBoard, i, width))

{

System.out.print("(Row)" + i + " Error Detected \n");

//Do something - The row has repetitions

}

for(int j = 0; j < height; j++)

if(!IsValidColumn(sudokuBoard, j, height))

{

System.out.print(" (Column)" + j + " Error Detected \n");

//Do something - The columns has repetitions

}

// for(int i=0; i<3; i++)

// if(!isBlock1Valid(sudokuBoard,width, height)){

// System.out.print("hi");

//}



}



static boolean isBlock1Valid(int[][] sudokuBoard, int referenceRow, int referenceColumn)

{

block1

boolean[] seen = new boolean[9];



for (int i = 0; i < 3; i++){



for (int j = 0; j < 3; j++){



if ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) return false;





else ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) = true;

}

}

return true;

}







this is my validate class that calls the boolean expression that i have implemented. I'm unsure the parameters to send the boolean in Validate. and thought about rearranging it so that i send Blocks of 3x3 dimensions instead.





and the c++ link Dividing a 9x9 2d array into 9 sub-grids (like in sudoku)? (C++)


Comments

  1. I'm giving you the basics with integer array examples. Assume you have a 9x9 2D array of integers. Your aim is dividing it into 9 3x3 2D array of integers, meaning you want to have a 3D array of integers in the end. This is what I understood from your question.

    Firstly, convert your 9x9 2D array into 1D array having 81 elements, than construct a 3D array with these elements.

    Scrutinize and test my code:

    public class Main
    {
    public static void main(String[] args)
    {
    int[] input = {1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,
    3,3,3,3,3,3,3,3,3,
    4,4,4,4,4,4,4,4,4,
    5,5,5,5,5,5,5,5,5,
    6,6,6,6,6,6,6,6,6,
    7,7,7,7,7,7,7,7,7,
    8,8,8,8,8,8,8,8,8,
    9,9,9,9,9,9,9,9,9};

    int[][][] output = get3DVersion(input);

    for(int i=0; i<output.length; i++)
    {
    for(int j=0; j<output[i].length; j++)
    {
    for(int k=0; k<output[j].length; k++)
    System.out.print(output[i][j][k] + " ");
    System.out.println();
    }
    System.out.println();
    }
    }

    public static int[][] get2DVersion(int[] input)
    {
    int[][] output = new int[9][9];

    for(int i=0; i<9; i++)
    for(int j=0; j<9; j++)
    output[i][j] = input[i*9+j];
    output[8][8] = input[80];

    return output;
    }

    public static int[][][] get3DVersion(int[] input)
    {
    int[][][] output = new int[9][3][3];
    int[][] newInput = get2DVersion(input);

    for(int i=0; i<9; i++)
    for(int j=0; j<3; j++)
    for(int k=0; k<3; k++)
    output[i][j][k] = newInput[i][j*3+k];

    output[8][2][2] = newInput[8][8];

    return output;
    }
    }

    ReplyDelete
  2. Try this code snippet, it can show you different indices inside a given block, as obtained by using int block = (((row / 3) * 3) + (column / 3));

    import java.util.Scanner;

    public class TwoDArray
    {
    public static void main(String... args) throws Exception
    {
    Scanner scanner = new Scanner(System.in);
    int row = 9;
    int column = 9;
    int[][] array = new int[row][column];

    for (int i = 0; i < row; i += 3)
    {
    for (int j = 0; j < column; j += 3)
    {
    /*
    * Here we are finding which block we are standing at.
    */
    int block = (((i / 3) * 3) + (j / 3));
    System.out.println("Block : " + block);
    for (int k = i; k < (i + 3); k++)
    {
    for (int l = j; l < (j + 3); l++)
    {
    // This is where you are getting your array inside the given block.
    System.out.print("[ " + k + " ][ " + l + " ]\t");
    }
    System.out.println();
    }
    }
    }
    System.out.println("Width is : " + array[0].length);
    System.out.println("Height is : " + array.length);
    }
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.