简体   繁体   English

打印没有重复的字符元素

[英]Printing Char Elements Without Duplicates

i have this char array 我有这个char数组

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

i would like to look for duplicate elements in the array and print the elements in the array only once like so: 我想在数组中查找重复的元素,并只在这样打印数组中的元素:

h,e,l,o,w,r,d H,E,L,O,W,R,d

i tried with this code 我试过这个代码

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

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

                        System.out.println("Duplicate");

                    } }

how can i achieve this? 我怎么能实现这个目标?

Try it using Java 8: Arrays.asList(c).stream().distinct().collect(Collectors.toList()); 尝试使用Java 8: Arrays.asList(c).stream().distinct().collect(Collectors.toList()); to get the array back try: Arrays.asList(c).stream().distinct().toArray(size -> new Char[size]); 要获取数组,请尝试: Arrays.asList(c).stream().distinct().toArray(size -> new Char[size]); Its all untested so Plesse let me know if these is something mit working. 所有未经测试的Plesse让我知道这些是否有效。 For printing add .foreach(System.out::println) 用于打印添加.foreach(System.out::println)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM