2016-06-15 70 views
0

我正在尝试制作游戏的桌面布局。每个桌子都有玩家数量。添加从另一个数组的值的长度的数组?

这些金额在表[]中。

表[0] = 9,表[1] = 9,表[2] = 8等等

表[]的长度为表的有量。

表[0](so 9)的值是玩家table1 []应该拥有的金额。

所以我得到2个数组:

tables[] 
table1[] 

表[0](第一值)9,我想表1的大小[]为9,这是可能的?

我试过一个while循环,但是我只能在循环中调用table1 [],而不是在外面。

是否可以从数组中填充多个整数?像

int table1 = tables[0] 
int table2 = tables[1] 

我不能调用在Table 1和Table与循环:

for(int i = 0; i < tables.length; i++){ 
tablei = tables[i] 
} 
+0

'表[1]'不是第一值是第二。计数从0开始。要创建一个包含9个元素的数组,可以这样做:'tables [] = new int [9]' –

回答

3

我试图while循环,但我只能当其在循环中,不被外部调用表1 []。

您可能在循环中定义了它。

假设数组包含整数,回答你的问题将是

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

编辑:我让你编辑的问题。你不能把一个变量放到一个名字里,并期望Java能够理解这个,所以tablei不起作用。你可能想在这里使用一个二维数组,也就是一个数组的数组。

为了您的理解,我将tables更改为tableSizes,因为它更好地描述了变量的用途,然后我可以使用tables作为二维数组。

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 

如前所述,我们从0开始在我的例子计算,所以你table1成为tables[0]

+0

你能帮我进一步看看我的编辑吗? – user1806846

+0

不知道您是否收到关于修改的通知,因此:done :-) – GreenThor

3

这?

double[] table1 = new double[table[0]]; 
1
int[][] playersPerTable = new int[tables.length][]; 
for (int tableIndex = 0; tableIndex < tables.length; ++tableIndex) { 
    playersPerTable[tableIndex] = new int[tables[tableIndex]]; 
} 
... 
playersPerTable[tableIndex][playerIndex] = 13; 

类,如ListArrayListMapSet可能会更有活力。 由于tables[i] == playersPerTable[i].length您可能不需要tables

1
import java.io.*; 


public class tables { 
    public static void main(String[] args) { 
     int[] tables = {9, 4, 5}; 
     int[] tables1 = new int[tables[0]]; 
     System.out.println(tables1.length); 
    } 
} 

输出:
9

相关问题