你可以创建一个行的代码,while循环中,这将创造一个新的数组和变化数组的名字与while循环的每次迭代?Java数组的创建和阵列名称更改
实施例:
int size = 10;
int name_count = 1;
while(size <= 100)
{
//name_count is changing the name of the array by calling it
// "array1", "array2", etc...
//I know this may not be correct code for changing the name of
// the array, but it is suppose to get the point across.
int[] array(name_count) = new int[size];
for (int i = 0; i <= size; i++)
{ /* Adding numbers to an array */ }
size = size + 5;
name_count++;
}
为什么要在循环的每次迭代中更改数组的名称?不知道如果得到这背后的逻辑。 :)无论如何,你的代码实际上并不会改变数组的名称 - 它会创建一个NEW数组,不包含任何旧数字。 – FloppyDisk 2012-03-26 16:35:49
我需要创建19个不同的数组,第一个数组包含10个有符号整数,每个数组的大小将增加5,直到达到100。因此,想要在每次迭代中更改数组名称。 – seiryuu10 2012-03-26 16:50:20
您是否正在尝试重新创建一个数据结构,比如ArrayList,它的数组大小随着更多元素的添加而增加?你有两种选择可以更有效地解决你的问题。首先,只需提前创建所有阵列。或者第二,每次你增加5时,用'size = oldsize + 5'创建一个新数组,然后将旧值复制到新数组中。这样你的数组会像ArrayList一样动态增长。 – FloppyDisk 2012-03-26 17:19:30