Java 如何打印出一个哈希集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20576839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:52:55  来源:igfitidea点击:

How to print out a hashset

javahashset

提问by user3097544

I just now started working with hashsets and I am trying to print it out but all it prints out is its location i tried Iterator and .toString() here is my code :

我刚刚开始使用哈希集,我正在尝试将其打印出来,但它打印出来的只是它的位置,我尝试了 Iterator 和 .toString() 这是我的代码:

char [] a = RandomWord.toCharArray();
        HashSet<char[]>  set = new HashSet<char[]>();
        set.add(a);
          Iterator<char[]> itr = set.iterator();
            while(itr.hasNext()){
                System.out.println(" Iterating over HashSet in Java current object: " + itr.next().toString());
            }
    }

采纳答案by Alexis C.

You have to print the content of your array. Arrays.toString(char[] a)will do that for you :

您必须打印数组的内容。Arrays.toString(char[] a)会为你做到这一点:

System.out.println(" Iterating over HashSet in Java current object: " + Arrays.toString(itr.next()));