This is because a double[][] is an array of double[] which you can't assign 0.0 to (it would be like doing double[] vector = 0.0). In fact, Java has no true multidimensional arrays.
As it happens, 0.0 is the default value for doubles in Java, thus the matrix will actually already be filled with zeros when you get it from new. However, if you wanted to fill it with, say, 1.0 you could do the following:
I don't believe the API provides a method to solve this without using a loop. It's simple enough however to do it with a for-each loop.
double[][] matrix = new double[20][4];
// Fill each row with 1.0
for (double[] row: matrix)
Arrays.fill(row, 1.0);
Answer from aioobe on Stack OverflowThis is because a double[][] is an array of double[] which you can't assign 0.0 to (it would be like doing double[] vector = 0.0). In fact, Java has no true multidimensional arrays.
As it happens, 0.0 is the default value for doubles in Java, thus the matrix will actually already be filled with zeros when you get it from new. However, if you wanted to fill it with, say, 1.0 you could do the following:
I don't believe the API provides a method to solve this without using a loop. It's simple enough however to do it with a for-each loop.
double[][] matrix = new double[20][4];
// Fill each row with 1.0
for (double[] row: matrix)
Arrays.fill(row, 1.0);
double[][] arr = new double[20][4];
Arrays.fill(arr[0], 0);
Arrays.fill(arr[1], 0);
Arrays.fill(arr[2], 0);
Arrays.fill(arr[3], 0);
Arrays.fill(arr[4], 0);
Arrays.fill(arr[5], 0);
Arrays.fill(arr[6], 0);
Arrays.fill(arr[7], 0);
Arrays.fill(arr[8], 0);
Arrays.fill(arr[9], 0);
Arrays.fill(arr[10], 0);
Arrays.fill(arr[11], 0);
Arrays.fill(arr[12], 0);
Arrays.fill(arr[13], 0);
Arrays.fill(arr[14], 0);
Arrays.fill(arr[15], 0);
Arrays.fill(arr[16], 0);
Arrays.fill(arr[17], 0);
Arrays.fill(arr[18], 0);
Arrays.fill(arr[19], 0);
Filling a 2D array in Java - Stack Overflow
How do you fill a 2D array with "cells"/coordinates?
[Java] How to fill an 2D array with different unique numbers?
java - Can I use Arrays.fill with a 2d array? If so how do I do that? - Stack Overflow
How do I use the `Arrays.fill()` in Java?
What are some limitations of the `Arrays.fill()` in Java method?
What are the benefits of using the `Arrays.fill()` method?
Videos
Arrays in Java are zero-based, which means they start at index zero, and range until index array.length-1.
Your code starts the row and column at 1—which means you're skipping the initialization of row/column 0. That's probably where at least some of the problems are coming from, since you're using your 5x5 array (rows/columns 0,1,2,3,4) as a 4x4 array (rows/columns 1,2,3,4).
There's also the fact that your Encrypt method doesn't actually make any assignments to the array. You probably want to initialize it like this:
// NOTE: changed return type to void -- this is a side-effect-only method!
public void Encrypt(char Encrypt[][])
{
// NOTE: use single-quotes for chars. double-quotes are for strings.
char aChar = 'A';
// NOTE: changed the starting loop values from `1` to `0`
for (int row = 0; row < Encrypt.length; row++)
{
// NOTE: technically Encrypt.length works here since it's a square
// 2D array, but you should really loop until Encrypt[row].length
for (int column = 0; column < Encrypt[row].length; column++)
{
// NOTE: set each entry to the desired char value
Encrypt[row][column] = aChar;
}
}
}
There are several issues with your original code. Look at the NOTE entries in the comments for individual explanations.
You are missing the most crucial part of what you are trying to accomplish.
Where are you setting your matrix to the letter A?
Change your Encrypt function to the following:
//filling the array with the letter A
public void Encrypt(char arr[][])
{
//char[] alpha = alphabets.toCharArray;
//declaring variable to fill array with A
char aChar = 'A';
for (int row = 0; row < arr.length; row++)
{
for (int column = 0; column < arr[row].length; column++)
{
arr[row][column] = aChar;
}
}
}
Just to be clear, I don't want to create a point object nor do I want to something like i + j etc.
For example, I want to have a 10 x 10 "grid" in order to work with union-find / disjoint set to generate a maze so I need the numbers, not objects. I may be overthinking this but how in the world does one create a grid to access a cell? Like, if I say maze[1][2], I would conceptually (not literally) expect to get 2,3 if that makes sense, but not an actual coordinate output of (2,3). I want to be able to unionize the cells to create a path.
Hello reddit, What I want to do is basically to fill a 2D array with unique numbers in every row and column. The numbers should be from 1 to 4. And as you may have guessed the array is 4x4.
I have the basics set up with a nested for loop,looping trough the array. Giving it ,at the moment, random but not unique values. I can not really wrap my head around the logic of checking the the values already in the array.
How would I proceed to fill the rows and columns with unique values?
Example of output:
-
1 2 3 4
-
2 3 4 1
-
3 4 1 2
-
4 1 2 3
Any help is appreciated!
To fill the first row in 2D array, use Arrays.fill, to fill the rest of the rows, use Arrays.copyOf.
Next, it's better to implement separate methods to perform different tasks:
- Create and fill the grid
- Print the grid
static String[][] fillGrid(int rows, int cols, String cell) {
String[][] grid = new String[rows][cols];
String[] row = new String[cols];
Arrays.fill(row, cell);
grid[0] = row;
for (int i = 1; i < rows; i++) {
grid[i] = Arrays.copyOf(row, cols);
}
return grid;
}
static void printGrid(String[][] grid) {
for (int i = 0; i < grid.length; i++) {
if (i == 0) {
System.out.print(" ");
for (int j = 0; j < grid[0].length; j++) {
System.out.printf("%2d ", j + 1);
}
System.out.println();
}
for (int j = 0; j < grid[i].length; j++) {
if (j == 0) {
System.out.printf("%2d:", i + 1);
}
System.out.printf("%2s ", grid[i][j]);
}
System.out.println();
}
}
Then they may be tested like this:
public static void main(String ... args) {
String[][] grid = fillGrid(8, 8, "x");
printGrid(grid);
}
The output will be as follows:
1 2 3 4 5 6 7 8
1: x x x x x x x x
2: x x x x x x x x
3: x x x x x x x x
4: x x x x x x x x
5: x x x x x x x x
6: x x x x x x x x
7: x x x x x x x x
8: x x x x x x x x
The "import java.util.arrays" needs to be above the class definition at the top of the file.