Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to create a 2d string array of initally repetitive data?

In Python, typically to define said game board I would have used the following code:

board = [['0','0','0','0','0','0','0','0','0','0'] for i in range(10)]

However, using Java the only way I have been able to find so far to produce the same result would be to do:

public static String[][] board = {
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"},
        {"0","0","0","0","0","0","0","0","0","0"}};

Surely there must be a more efficient way of doing this. I know I can do it efficiently if it were to be integers, but it has to be strings given the situation. I had thought a for loop would also have applied, but I can't seem to find the right syntax for it; perhaps it isn't applicable for Java. Are there any suggestions?

like image 218
user15292893 Avatar asked Feb 26 '21 21:02

user15292893


People also ask

How do you create a 2D array of strings?

int columns = 2; int rows = 2; Here you are using the type String[][] to create a new 2D array with the size defined by [rows][columns] . String[][] newArray = new String[columns][rows]; You assign the values by its placement within the array.

How do you create a 2D array?

To create an array use the new keyword, followed by a space, then the type, and then the number of rows in square brackets followed by the number of columns in square brackets, like this new int[numRows][numCols] . The number of elements in a 2D array is the number of rows times the number of columns.

How to initialize a 2D array of strings in C?

The two dimensional (2D) array of Strings in C also can be initialized as, Since it is a two-dimension of characters, so each String (1-D array of characters) must end with null character i.e. ‘\0’

How are longitude and latitude represented in a 2D array?

Longitude is represented by the columns in the 2D array and latitude is represented by each row in the 2D array.The creator of this 2D array would like to look at the height of the city park along the longitude 100.0 - what traversal method would make the most sense in order to do so?

How to read and Display 2D array of strings in C?

Reading and displaying 2d array of strings in C. The two-dimensional array of strings can be read by using loops. To read we can use scanf (), gets (), fgets () or any other methods to read the string. The two-dimensional array of strings can be displayed by using loops.

How to read two-dimensional array of strings in Python?

The two-dimensional array of strings can be read by using loops. To read we can use scanf (), gets (), fgets () or any other methods to read the string. The two-dimensional array of strings can be displayed by using loops.


3 Answers

You can loop over each row and use Arrays.fill:

public static String[][] board = new String[rows][cols];
static {
    for (String[] row : board) Arrays.fill(row, "0");
}

Demo

like image 173
Unmitigated Avatar answered Oct 25 '22 14:10

Unmitigated


You can use nested for loops to create the 10 rows and 10 columns. So within the first loop (which creates a "row"), you'd add another loop to fill the "columns."

String[][] board = new String[10][10];

// 10 rows
for (int i = 0; i < 10; i++) {
    // 10 columns
    for (int j = 0; j < 10; j++) {
        // Set the value of this row/column
        board[i][j] = "0";
    }
}

This obviously isn't as elegant as Python's one-line solution, but it does what you need.

There are other ways to do this (such as just adding a full 10-digit array to the board array in each loop), but I find this to be best for scalability and readability.


For further clarity, if you may possibly need to change the number of rows or columns in the future, it would be better to move those values to variables and use those instead:

int rows = 10;
int cols = 10;

String[][] board = new String[rows][cols];

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; i++) {
        board[i][j] = "0";
    }
}

This way you only need to update the variables instead of also modifying the for loops and array declaration.

like image 44
Zephyr Avatar answered Oct 25 '22 14:10

Zephyr


You can do it like this. Since Strings are immutable, it's safe to use clone() here. Thanks to @iota for suggesting setAll

int n = 10;
String[][] arr = new String[n][n];
Arrays.fill(arr[0], "0");
Arrays.setAll(arr, i->arr[0].clone());


for(String[] a : arr) {
    System.out.println(Arrays.toString(a));
}

Prints

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
like image 1
WJS Avatar answered Oct 25 '22 13:10

WJS