Syntax For Creating a Two-Dimensional Array in Java

Syntax for creating a two-dimensional array in Java

Try the following:

int[][] multi = new int[5][10];

... which is a short hand for something like this:

int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];

Note that every element will be initialized to the default value for int, 0, so the above are also equivalent to:

int[][] multi = new int[][]{
{ 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 }
};

How can you make a two-dimensional product table array in java?

First, you will need to initialize a two-dimensional array using the new int[numRows][numCols] syntax. Then, use a nested loop to iterate through each row and column, filling each slot with the multiplication of the row and column indexes.

public static int[][] makeSquareMultiplicationTable(int sideLength) {
int[][] table = new int[sideLength][sideLength];
for (int row = 1; row <= sideLength; row++) {
for (int col = 1; col <= sideLength; col++) {
table[row-1][col-1] = row * col;
}
}
return table;
}

Dimensions for two-dimensional array creation in Java seem backwards

An expression like new int[2][5] will create an outer array of length 2, with each element referencing an array of length 5, so a total of 3 arrays are created. This is well-documented in the Java Language Specification, Example 15.10.2-2. Multi-Dimensional Array Creation:

The declaration:

float[][] matrix = new float[3][3];

is equivalent in behavior to:

float[][] matrix = new float[3][];
for (int d = 0; d < matrix.length; d++)
matrix[d] = new float[3];

It does not mean (int[2])[5], i.e. an array of 5 referencing arrays of 2.

Because of that, the expression to lookup values is evaluated left-to-right, e.g. x[2][5] means (x[2])[5], i.e. in int[][] x variable, lookup x[2], which is an int[], and in that array lookup value at index 5.

int[][] x = new int[9][9];
// z = x[2][5]
int[] y = x[2];
int z = y[5];

Initialising a 2D array in Java

Yes, you are correct the second way of creating a 2D array will not work because when the compiler will compile the code because it couldn't decide its type.

How can I formulate two dimensional arrays in Java?

I think out there must be a lot of better solutions than this, but you can try it:

int[][] array = new int[5][5];

int value = 1, flag = 0;

for (int i = 0; i < 5; i++) {
if (flag == 0) {
for (int j = 0; j < 5; j++) {
array[j][i] = value++;
}
flag = 1;
} else {
for (int j = 4; j >= 0; j--) {
array[j][i] = value++;
}
flag = 0;
}
}

for (int i = 0; i < 5; i++) {
System.out.println(Arrays.toString(array[i]));
}

This snippet will print your desired output.

How to create an irregular shaped two dimensional array in Java

Would the best method be to just create a typical 5x5 square array and place NULL values in the 6 empty cells?

Yes, I think it would be the most intuitive solution. Keep in mind you need to distinguish an empty call and empty card cell, so you can't use null for both. It shouldn't be a problem: a constant instance Square.EMPTY would help.

A two-dimensional array is the best data structure for this kind of games. If you can come up with a method that tells what the boundaries are and how to iterate over the board nicely, you will be fine.

Or is there a more sophisticated way to do this for oddly shaped 2D arrays?

I can't think of any. You could have

Square[][] board = new Square[MAX][];

board[2] = new Square[MAX];
board[3] = new Square[NOT_MAX];

but for arrays of length NOT_MAX, you additionally need to store an offset. It complicates things and assumes the pattern stays always as simple as you've shown. For a pattern with gaps in between, it won't work.

Creating two-dimensional arrays using nested-loop

It loops through the 2 dimensional array.
If i is equal to j, e.g ( 0,0 1,1 2,2 ) then it adds grade[i][j] with grade[j][i]. Since i and j are equal it adds the location with itself.

When i is not equal to j it multiplies grade[i][j] with grade[j][i].

Since they are not equal it multiplies 2 different positions in the grid.
e.g.

grade [3][1] is multiplied by grade[1][3], not by itself.

If you changed grade[1][3] to 2, then all corners would be output as 2

The input:

1 0 2
0 1 0
1 0 1

would output:

2 0 2
0 2 0
2 0 2

how to create dynamic two dimensional array in java?

Since the number of columns is a constant, you can just have an List of int[].

    import java.util.*;
//...

List<int[]> rowList = new ArrayList<int[]>();

rowList.add(new int[] { 1, 2, 3 });
rowList.add(new int[] { 4, 5, 6 });
rowList.add(new int[] { 7, 8 });

for (int[] row : rowList) {
System.out.println("Row = " + Arrays.toString(row));
} // prints:
// Row = [1, 2, 3]
// Row = [4, 5, 6]
// Row = [7, 8]

System.out.println(rowList.get(1)[1]); // prints "5"

Since it's backed by a List, the number of rows can grow and shrink dynamically. Each row is backed by an int[], which is static, but you said that the number of columns is fixed, so this is not a problem.



Related Topics



Leave a reply



Submit