println(Array(11,12,13,14,15).filter(myf2).toList) def myf2(n:Int):Boolean={ var flag:Boolean=true for(i<-2 to n/2) if(n%i==0) flag=false flag } 输出结果是()
时间: 2024-04-05 17:31:17 浏览: 15
输出结果应该是 List(11, 13, 15),因为 myf2 是一个判断一个数是否为质数的函数,传入 filter 中,会筛选出数组中满足条件(即为质数)的元素,然后将它们转换为 List。在这个例子中,11、12、13、14、15 中只有 11、13、15 是质数,因此最终输出的是 List(11, 13, 15)。
相关问题
for (int i : array){\n System.out.println(i);\n }
This is a for-each loop in Java, which is used to iterate over an array or a collection. The loop iterates over each element in the array and assigns it to the variable "i". The code inside the loop then executes, which in this case is simply printing out the value of "i". The "\n" is a newline character, which is used to print each value on a new line.
for (int z:array) { System.out.println(z); }解释
这段代码使用了Java中的增强型for循环语句,也称为foreach语句。它可以用来遍历数组或集合等数据结构中的元素。具体来说,这段代码的意思是:
1. 对于数组array中的每个元素z,执行花括号中的代码。
2. 在循环的每次迭代中,将当前元素的值赋值给变量z,并执行System.out.println(z)语句打印该元素的值。
因此,这段代码可以用来打印数组array中的所有元素。输出结果是:
```
99
2
4
5
6
```