【发布时间】:2013-01-06 08:03:09
【问题描述】:
我正在尝试将我生成的所有随机数放入 arraylist 并将所有随机数打印到单个数组中
static int pick;
public static void main(String[] args) {
ArrayList al = new ArrayList();
Random rand = new Random();
for (int j = 0; j<10; j++)
{
pick = rand.nextInt(100);
al.add(pick);
System.out.println("Contents of al: " + al);
}
从上面的函数我得到了这样的结果
Contents of al: [43]
Contents of al: [43, 44]
Contents of al: [43, 44, 3]
Contents of al: [43, 44, 3, 66]
Contents of al: [43, 44, 3, 66, 47]
Contents of al: [43, 44, 3, 66, 47, 24]
Contents of al: [43, 44, 3, 66, 47, 24, 12]
Contents of al: [43, 44, 3, 66, 47, 24, 12, 35]
Contents of al: [43, 44, 3, 66, 47, 24, 12, 35, 0]
Contents of al: [43, 44, 3, 66, 47, 24, 12, 35, 0, 85]
我想要的预期输出只是上面结果的最后一行
Contents of al: [43, 44, 3, 66, 47, 24, 12, 35, 0, 85]
有人知道怎么做吗?
【问题讨论】:
-
将
System.out.println语句移到 for 循环之外。 -
好的......谢谢......它正在工作......只要意识到就这么简单:D
标签: java arrays random arraylist