簡體   English   中英

打印沒有重復的字符元素

[英]Printing Char Elements Without Duplicates

我有這個char數組

char[] c = new char[]{'h','e','l','l','o','w','o','r','l','d';

我想在數組中查找重復的元素,並只在這樣打印數組中的元素:

H,E,L,O,W,R,d

我試過這個代碼

for (int j = 0; j < c.length; j++) {

                    if (c[j] == c[j]) {

                        System.out.println("Duplicate");

                    } }

我怎么能實現這個目標?

嘗試使用Java 8: Arrays.asList(c).stream().distinct().collect(Collectors.toList()); 要獲取數組,請嘗試: Arrays.asList(c).stream().distinct().toArray(size -> new Char[size]); 所有未經測試的Plesse讓我知道這些是否有效。 用於打印添加.foreach(System.out::println)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM