共計 392 個字符,預計需要花費 1 分鐘才能閱讀完成。
可以使用 HashSet 來實現輸出不重復的元素。HashSet 是 Java 中的一個集合類,它可以存儲不重復的元素。
下面是一個示例代碼:
import java.util.HashSet;
public class Main {public static void main(String[] args) {int[] array = {1, 2, 3, 2, 4, 5, 1, 6, 6};
HashSet set = new HashSet();
for (int i = 0; i < array.length; i++) {set.add(array[i]);
}
for (int element : set) {System.out.println(element);
}
}
}
運行上述代碼,輸出結果為:
1
2
3
4
5
6
其中,使用 HashSet 來存儲數組中的元素,由于 HashSet 不允許存儲重復的元素,所以最后輸出的結果就是不重復的元素。
丸趣 TV 網 – 提供最優質的資源集合!
正文完