user1806846 - 5 months ago 9

Java Question

I am trying to make a tablelayout for a game. Each table has amount of players.

These amounts are in tables[].

tables[0] = 9, tables[1] = 9, tables[2] = 8 etc.

The length of tables[] is the amount of tables there are.

the value of tables[0] (so 9) is the amount of players table1[] should have.

So i got 2 arrays:

`tables[]`

table1[]

Tables[0] (first value) is 9, i want the size of table1[] to be 9, is this possible?

I tried a while loop, but i can only call the table1[] when its in the loop, not outside.

Is it possible to fill multiple ints from an array? like

`int table1 = tables[0]`

int table2 = tables[1]

i cant call table1 and table2 in a for loop with:

`for(int i = 0; i < tables.length; i++){`

tablei = tables[i]

}

Answer

I tried a while loop, but i can only call the table1[] when its in the loop, not outside.

You probably defined it in the loop.

Assuming the arrays contains integers, the answer to your question would be

```
int[] table1 = new int[tables[0]];
```

**EDIT:** I get your edited problem. You can't put a variable into a name and expect Java to understand that, so `tablei`

doesn't work. You may want to use a 2-dimensional array here, that is, an array of an array.

For your understanding, I changed what you called `tables`

to `tableSizes`

as it describes the variable's purpose better and I can use `tables`

as the 2-dimensional array then.

```
int[] tableSizes = new int[9];
tableSizes[0] = 9;
tableSizes[1] = 9;
tableSizes[2] = 8;
// and so on
int[][] tables = new int[9][]; // create the 'outer' array
for (int i = 0; i < 9; i++) {
tables[i] = new int[tableSizes[i]]; // create the 'inner' arrays
}
// access like this
int var = tables[0][1]; // second value of first table
tables[1][0] = 1; // first value of second table
```

As mentioned, we start counting with 0, so in my example your `table1`

became `tables[0]`

.