使用随机数调整整数数组的大小
Resizing an Integer Array with Random Numbers
我正在尝试创建一个 Integer[],每个循环增加 10 的倍数。设置 Integer[] 大小后,我希望它用随机整数填充数组。我能够增加数组的大小,但其中存储的值为空。
对我来说,这意味着数组正在正确调整大小,但它们的元素没有被分配给任何东西。我正在尝试双 for 循环,内部循环分配随机值。如果有更好的方法来做到这一点(我确定有 b/c 我的不是 运行!)你能帮忙吗?
这是我创建的 Int[]
public class TimeComplexity {
Integer[] data;
public TimeComplexity()
{
Random random = new Random();
for (int N = 1000; N <= 1000000; N *= 10)
{
N = random.nextInt(N);
data = new Integer[N];
//checking to see if the random numbers were added.
//array size is okay but locations aren't taking a
//random number
System.out.println(Arrays.toString(data));
}
}
如果您对它的输出感兴趣,我的主要 class。 (这不是问题的一部分,但如果你有建议,我会喜欢的!)
public class TimeComplexityApp {
private static int MAXSIZE = 1000000;
private static int STARTSIZE = 1000;
public TimeComplexityApp()
{
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
TimeComplexity time = new TimeComplexity();
System.out.println(time);
System.out.printf("%-6s %13s %13s\n\n\n","ARRAY","int","INTEGER");
for (int N = STARTSIZE; N <= MAXSIZE; N *= 10)
{
double d = 1.0;
System.out.printf("\n%-6d %15.2f %15.2f\n", N, d, d);
}
}
}
在循环的每次迭代中,您都在创建一个随机(整数大小)长度的新数组。但你永远不会把任何东西放进去。正确的做法是:
int[] vals = ...;
for (int i = 0; i < end - start; i++) {
if (vals.length < i; i++) {
//1. create new larger int[]
//2. copy the old array into the new array
//3. vals = yourNewArray
}
vals[i] = random.nextInt();
}
在显示的第一个源代码中缺少初始化整数数组的元素。
数据=新整数[N];仅创建大小为 N 的整数数组,缺少包含数组每个单元格中的元素。
因此,只需要一个循环来完成每个元素或元胞数组:
for (int i = 0; i <N; i ++)
data [i] = random.nextInt (N);
现在这个数组是完整的,不会 return 每一项都为 NULL。
我正在尝试创建一个 Integer[],每个循环增加 10 的倍数。设置 Integer[] 大小后,我希望它用随机整数填充数组。我能够增加数组的大小,但其中存储的值为空。 对我来说,这意味着数组正在正确调整大小,但它们的元素没有被分配给任何东西。我正在尝试双 for 循环,内部循环分配随机值。如果有更好的方法来做到这一点(我确定有 b/c 我的不是 运行!)你能帮忙吗?
这是我创建的 Int[]
public class TimeComplexity {
Integer[] data;
public TimeComplexity()
{
Random random = new Random();
for (int N = 1000; N <= 1000000; N *= 10)
{
N = random.nextInt(N);
data = new Integer[N];
//checking to see if the random numbers were added.
//array size is okay but locations aren't taking a
//random number
System.out.println(Arrays.toString(data));
}
}
如果您对它的输出感兴趣,我的主要 class。 (这不是问题的一部分,但如果你有建议,我会喜欢的!)
public class TimeComplexityApp {
private static int MAXSIZE = 1000000;
private static int STARTSIZE = 1000;
public TimeComplexityApp()
{
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
TimeComplexity time = new TimeComplexity();
System.out.println(time);
System.out.printf("%-6s %13s %13s\n\n\n","ARRAY","int","INTEGER");
for (int N = STARTSIZE; N <= MAXSIZE; N *= 10)
{
double d = 1.0;
System.out.printf("\n%-6d %15.2f %15.2f\n", N, d, d);
}
}
}
在循环的每次迭代中,您都在创建一个随机(整数大小)长度的新数组。但你永远不会把任何东西放进去。正确的做法是:
int[] vals = ...;
for (int i = 0; i < end - start; i++) {
if (vals.length < i; i++) {
//1. create new larger int[]
//2. copy the old array into the new array
//3. vals = yourNewArray
}
vals[i] = random.nextInt();
}
在显示的第一个源代码中缺少初始化整数数组的元素。
数据=新整数[N];仅创建大小为 N 的整数数组,缺少包含数组每个单元格中的元素。
因此,只需要一个循环来完成每个元素或元胞数组:
for (int i = 0; i <N; i ++)
data [i] = random.nextInt (N);
现在这个数组是完整的,不会 return 每一项都为 NULL。