现有一个Integer数组 Integer[] arr = {1, 2, 3, 2, 3, 2, 2, 4, 2};/请使用流操作获取其中无重复的元素(重复元素仅保留一个),保存在一个新数组中。并打印新数组中的元素。
时间: 2023-04-07 21:02:51 浏览: 43
可以回答这个问题。使用流操作可以通过以下代码实现:
Integer[] arr = {1, 2, 3, 2, 3, 2, 2, 4, 2};
Integer[] distinctArr = Arrays.stream(arr).distinct().toArray(Integer[]::new);
System.out.println(Arrays.toString(distinctArr));
输出结果为:[1, 2, 3, 4]
相关问题
fortran中arr = [1, 2, 3],取出最后一个数
Fortran 中没有内置的数组数据类型,因此需要使用数组库(如 `ISO_FORTRAN_ENV` 或 `Fortran 90`)来处理数组。以下是使用 `ISO_FORTRAN_ENV` 库取出一个一维数组的最后一个元素的示例代码:
```fortran
use ISO_FORTRAN_ENV
integer, dimension(3) :: arr = [1, 2, 3]
integer :: last_elem
last_elem = arr(size(arr))
print *, last_elem ! 输出 3
```
在上面的代码中,我们首先使用 `use ISO_FORTRAN_ENV` 语句导入了 `ISO_FORTRAN_ENV` 库。然后,我们声明了一个名为 `arr` 的一维整数数组,包含元素 `[1, 2, 3]`。接下来,我们使用 `size` 函数获取 `arr` 数组的大小(即元素个数),并将其作为下标来取出最后一个元素,并将其存储在变量 `last_elem` 中。最后,我们将 `last_elem` 的值打印到控制台上,输出为 3,表示 `arr` 数组的最后一个元素是 3。
fortran中arr = [1, 2, 3],用函数计算维度
Fortran 中没有内置的数组数据类型,因此需要使用数组库(如 `ISO_FORTRAN_ENV` 或 `Fortran 90`)来处理数组。以下是使用 `ISO_FORTRAN_ENV` 库计算一个一维数组的维度的示例代码:
```fortran
use ISO_FORTRAN_ENV
integer, dimension(3) :: arr = [1, 2, 3]
integer :: dim
dim = size(arr)
print *, dim ! 输出 3
```
在上面的代码中,我们首先使用 `use ISO_FORTRAN_ENV` 语句导入了 `ISO_FORTRAN_ENV` 库。然后,我们声明了一个名为 `arr` 的一维整数数组,包含元素 `[1, 2, 3]`。接下来,我们使用 `size` 函数计算了 `arr` 数组的维度,并将其存储在变量 `dim` 中。最后,我们将 `dim` 的值打印到控制台上,输出为 3,表示 `arr` 是一个一维数组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)