创建随机数组
代码如下(示例):
import java.util.Random;
public class Test2 {
public static void main(String[] args) {
Random random = new Random();//创建一个random对象
int[] arr = new int[10];//创建一个int类型且容量为10的数组
for (int i = 0; i < arr.length; i++) {//给予下标,方便加入随机生成的int整数
arr[i] = (int) (random.nextInt(100));//将生成好的int类型整数添加到数组
}
for(int z :arr) {//采用加强for循环遍历添加好的随机数组元素
System.out.print(z+" ");
}
}
}
代码如下(输出):
67 84 96 61 39 13 62 52 38 96
总结
其他的有序集合(可以下标访问的集合)都可以用这个方法