How do I printout my 2 dimensional array? - java

Trying to build a data structure for a sudoku board, and I want to print it to the console for starters. I'm using a 2 dimensional array for represent each cell on a sudoku game and I'm having a really nooby problem printing it out.
public class Main {
public static void main(String[] args) {
int[][] puzzleBoard = {};
//Row 1
puzzleBoard[1][1] = 0;
puzzleBoard[2][1] = 0;
puzzleBoard[3][1] = 0;
puzzleBoard[4][1] = 0;
puzzleBoard[5][1] = 0;
puzzleBoard[6][1] = 0;
puzzleBoard[7][1] = 0;
puzzleBoard[8][1] = 0;
puzzleBoard[9][1] = 0;
//Row 2
puzzleBoard[1][2] = 0;
puzzleBoard[2][2] = 0;
puzzleBoard[3][2] = 0;
puzzleBoard[4][2] = 0;
puzzleBoard[5][2] = 0;
puzzleBoard[6][2] = 0;
puzzleBoard[7][2] = 0;
puzzleBoard[8][2] = 0;
puzzleBoard[9][2] = 0;
//Row 3
puzzleBoard[1][3] = 0;
puzzleBoard[2][3] = 0;
puzzleBoard[3][3] = 0;
puzzleBoard[4][3] = 0;
puzzleBoard[5][3] = 0;
puzzleBoard[6][3] = 0;
puzzleBoard[7][3] = 0;
puzzleBoard[8][3] = 0;
puzzleBoard[9][3] = 0;
//Row 4
puzzleBoard[1][4] = 0;
puzzleBoard[2][4] = 0;
puzzleBoard[3][4] = 0;
puzzleBoard[4][4] = 0;
puzzleBoard[5][4] = 0;
puzzleBoard[6][4] = 0;
puzzleBoard[7][4] = 0;
puzzleBoard[8][4] = 0;
puzzleBoard[9][4] = 0;
//Row 5
puzzleBoard[1][5] = 0;
puzzleBoard[2][5] = 0;
puzzleBoard[3][5] = 0;
puzzleBoard[4][5] = 0;
puzzleBoard[5][5] = 0;
puzzleBoard[6][5] = 0;
puzzleBoard[7][5] = 0;
puzzleBoard[8][5] = 0;
puzzleBoard[9][5] = 0;
//Row 6
puzzleBoard[1][6] = 0;
puzzleBoard[2][6] = 0;
puzzleBoard[3][6] = 0;
puzzleBoard[4][6] = 0;
puzzleBoard[5][6] = 0;
puzzleBoard[6][6] = 0;
puzzleBoard[7][6] = 0;
puzzleBoard[8][6] = 0;
puzzleBoard[9][6] = 0;
//Row 7
puzzleBoard[1][7] = 0;
puzzleBoard[2][7] = 0;
puzzleBoard[3][7] = 0;
puzzleBoard[4][7] = 0;
puzzleBoard[5][7] = 0;
puzzleBoard[6][7] = 0;
puzzleBoard[7][7] = 0;
puzzleBoard[8][7] = 0;
puzzleBoard[9][7] = 0;
//Row 8
puzzleBoard[1][8] = 0;
puzzleBoard[2][8] = 0;
puzzleBoard[3][8] = 0;
puzzleBoard[4][8] = 0;
puzzleBoard[5][8] = 0;
puzzleBoard[6][8] = 0;
puzzleBoard[7][8] = 0;
puzzleBoard[8][8] = 0;
puzzleBoard[9][8] = 0;
//Row 9
puzzleBoard[1][9] = 0;
puzzleBoard[2][9] = 0;
puzzleBoard[3][9] = 0;
puzzleBoard[4][9] = 0;
puzzleBoard[5][9] = 0;
puzzleBoard[6][9] = 0;
puzzleBoard[7][9] = 0;
puzzleBoard[8][9] = 0;
puzzleBoard[9][9] = 0;
int rows = 9;
int columns = 9;
int i, j;
for (i = 1; i < rows; i++){
for(j = 1; j < columns; j++){
System.out.print(puzzleBoard[i][j] + " ");
}
System.out.println( "" );
}
}
}

You didn't specify a size for your array.
int[][] puzzleBoard = {}; should be int[][] puzzleBoard = new int[10][10];
There is no need to initialize your array with a for loop or line by line if you're only filling it with 0.
Then arrays are 0 base indexed. So the first element will be at position [0][0] and the last one at position [9][9].
You could also remove your two variables rows and columns.
Finally, you should modify your for loop to start from 0.
for (i = 0; i < puzzleBoard.length; i++){
for(j = 0; j < puzzleBoard[i].length; j++){
System.out.print(puzzleBoard[i][j] + " ");
}
System.out.println( "" );
}
To learn more about arrays in Java, I advise you to read this.

You should initialize the array elements with a for loop as well!
Also, remember, Java arrays are zero based, so the topmost cell would be puzzleboard[0][0]!
You should also specify a size for your array - int[][] puzzleBoard = new int[10][10];

You should use java.util.Arrays.deepToString() to print 2D arrays:
Returns a string representation of the "deep contents" of the
specified array. If the array contains other arrays as elements, the
string representation contains their contents and so on. This method
is designed for converting multidimensional arrays to strings.
The string representation consists of a list of the array's elements,
enclosed in square brackets ("[]"). Adjacent elements are separated by
the characters ", " (a comma followed by a space). Elements are
converted to strings as by String.valueOf(Object), unless they are
themselves arrays.

Related

efficient way to do nested for loop and add to list

Below I have written some code that works and prints the right result, but is there a much more efficient way to do this using nested for loops, instead of having 5 different for loops?
String A = "";
String B = "";
String C = "";
String D = "";
String E = "";
ArrayList<String> id = new ArrayList<String>();
int row = 5;
for (int i=0; i < row; i++) {
A = "id0"+i;
id.add(A);
}
for (int i=0; i < row; i++) {
B = "id1"+i;
id.add(B);
}
for (int i=0; i < row; i++) {
C = "id2"+i;
id.add(C);
}
for (int i=0; i < row; i++) {
D = "id3"+i;
id.add(D);
}
for (int i=0; i < row; i++) {
E = "id4"+i;
id.add(E);
}
return id;
result
[id00, id01, id02, id03, id04, id10, id11, id12, id13, id14, id20, id21, id22, id23, id24, id30, id31, id32, id33, id34, id40, id41, id42, id43, id44]
The following should produce the same output by using a nested loop
ArrayList<String> id = new ArrayList<>();
int row = 5;
for (int i = 0; i < row; i++) {
for (int j = 0; j < row; j++) {
id.add("id" + i + j);
}
}
return id;
Consider the following:
ArrayList<String> id = new ArrayList<String>();
int row=5;
for (int i=0; i < 5; i++) {
for (int j=0; j < row; j++) {
String temp = "id" + i + j;
id.add(temp);
}
}
This simplifies the code into 2 nested for loops, with a single string variable instead of 5. This also shouldn't change the time complexity of your solution since the outer loop is constant.

How to Create 2D Array with #1 in Random Positions for Specific Number of Times

I want to create an 8x8 array with JAVA, in which i want to have 1 eight times in random generated positions. All the other positions of the array are going to be 0. I am using this code but obviously it is not filling the array with 1 for a specific number of times.
public static void main(String[] args) {
int [][] arr = new int [8][8];
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
arr[i][j] = (int) (Math.random()*2);
}
}
for(int k = 0; k < 8; k++){
for(int l = 0; l < 8; l++){
System.out.print(arr[k][l] + " ");
}
System.out.println();
}
}
}
import java.util.Random;
int[][] array = new int[8][8];
Random r = new Random();
int a = r.nextInt(8);
int b = r.nextInt(8);
//insert 8 random 1's in the 8x8 matrix, no duplicates
//by default in Java the other places are
for(int i = 0; i < 8; i++){
while(array[a][b] == 1){
a = r.nextInt(8);
b = r.nextInt(8);
}
array[a][b] = 1;
}

Why doesn't this second code work in which i try to insert the elements inside the 2d array?

Problems with 2D array.
This code works but the other code(below this) does not, when i try to insert the elements in the array.
`int [][] array = { {1,2,3},
{4,5,6},
{7,8,9},
{0}
};
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}`
This code does not work.
`int [][] arr;
array[0][0] = 1;
array[0][1] = 10;
array[0][2] = 100;
array[1][1] = 99;
array[1][2] = 123;
array[1][0] = 177;
array[2][1] = 999;
array[2][2] = 453;
array[3][0] = 000;
array[3][1] = 1090;
array[3][2] = 1202;`
for(int x = 0; x < arr.length; x++) {
for(int y = 0; y < arr[x].length; y++) {
System.out.print(arr[x][y]);
}
System.out.println();
}
This are the errors that i get:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The local variable arr may not have been initialized
The local variable arr may not have been initialized
The local variable arr may not have been initialized
Two issues. You named that array arr (not array), and you did not size it at declaration time. Like,
int [][] array = new int[4][3];
array[0][0] = 1;
array[0][1] = 10;
array[0][2] = 100;
array[1][1] = 99;
array[1][2] = 123;
array[1][0] = 177;
array[2][1] = 999;
array[2][2] = 453;
array[3][0] = 000;
array[3][1] = 1090;
array[3][2] = 1202;
for(int x = 0; x < array.length; x++) {
for(int y = 0; y < array[x].length; y++) {
System.out.print(array[x][y]);
}
System.out.println();
}

java - slow calculation processing

I try to run this, but it is very slow. Takes ages till it processes and finishes the calculating. Is there anyways I could improve it or make it work faster and more efficiently?
int n = 25;
int len = (int) Math.pow(2, n);
String[][] BinaryNumbers = new String[len][];
int[] DummyArray = new int[n];
int[][] BinaryNumbersInt = new int[len][];
for (int count = 0; count < len; count++) {
BinaryNumbers[count] = String.format("%" + n + "s",
Integer.toBinaryString(count)).replace((" "), ("0"))
.split("");
for (int i = 0; i < n; i++) {
DummyArray[i] = Integer.parseInt(BinaryNumbers[count][i]);
}
BinaryNumbersInt[count] = Arrays.copyOf(DummyArray, DummyArray.length);
}
thanks!
You're doing a lot of useless string manipulation. Try something like this:
for (int count = 0; count < len; count++) {
int[] DummyArray = new int[n];
int x = count;
for (int i = 0; i < n; i++) {
DummyArray[n-i-1] = x & 1;
x >>= 1;
}
BinaryNumbersInt[count] = DummyArray;
}

ArrayList<int[]> displaying last array only

I'm trying to write a "Mastermind" AI program and at the moment I'm trying to implement a naive AI which searches all possible 1296 combination of 4 pegs with 6 colours.
I have written up the following for loop to print out all combinations:
int compguess[] = new int[4];
int a, b, c, d;
ArrayList<int[]> combination = new ArrayList<int[]>();
for (int z = 0; z < 6; z++) {
for (int x = 0; x < 6; x++) {
for (int i = 0; i < 6; i++) {
for (int k = 0; k < 6; k++) {
a = z;
b = x;
c = i;
d = k;
compguess[0] = a;
compguess[1] = b;
compguess[2] = c;
compguess[3] = d;
combination.add(compguess);
When I run this code with System.out.println("combination" + Arrays.toString(combination.get(k))); at the end. This displays the combinations properly, however when I try to do the following:
for(int i=0; i< height; i++){
int[] temp = combination.get(i);
for(int j = 0; j < 4 ; j++){
state[i][j] = temp[j];
}
guess.addActionListener(this);
}
It only displays the last element (4,4,4,4) 40 times, instead I want it to be (0,0,0,0), (0,0,0,1), (0,0,0,2), (0,0,0,3), (0,0,0,4), (0,0,0,5), (0,0,1,0), (0,0,1,1), (0,0,1,2), (0,0,1,3) only 10 which is the size of the height
The problem is that you are using the same array every time, causing a change to one of them to change all, as they are, in fact, the same. Just reinitialize the array in the innermost for loop:
for (int z = 0; z < 6; z++) {
for (int x = 0; x < 6; x++) {
for (int i = 0; i < 6; i++) {
for (int k = 0; k < 6; k++) {
compguess = new int[4];
// rest of your code

Categories