编写一段代码,功能如下:现在一个List集合对象中循环加入1-9共计9个数值,接着,使用迭代窗口将List中的各数值的平方值依次输出。要求:对集合对象要使用泛型。
1
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 1; i < 10; i++) {
list.add(i);
}
Iterator<Integer> iterator=list.iterator();
while (iterator.hasNext()){
int iValue=iterator.next();
System.out.println(iValue);
}
}
编写程序,创建长度为 10 的整型数组。使用 1-100 之间的随机数填充数组,在
该数组中找出距离最小的两个相邻数值,输出原数组,最小距离值,以及满足最小
距离的两个相邻元素。
例如,如果数组为 a=[ 4, 8, 6, 1, 2, 9,12,4],则最小距离为 1 (1 和 2 之
间的距离)。 第一个数为 1,即 a[3]; 第二个数为 2,即 a[4]。如果数组为 a=[ 8, 4,
1, 8, 20, 32,15,10],则最小距离为 3 (4 和 1 之间的距离). 第一个数为 4,即
a[1]; 第二个数为 1,即 a[2]。