Java: Converting a set to an array for String representation

前端 未结 6 1049
遇见更好的自我
遇见更好的自我 2021-02-05 02:08

From Sun\'s Java Tutorial, I would have thought this code would convert a set into an array.

import java.util.*;

public class Blagh {
    public static void mai         


        
6条回答
  •  长发绾君心
    2021-02-05 02:43

    It's OK.

    You are not seeing the array contents with System.out.println(array) because println calls object.toString() to get the bytes from an Object for output.

    Since HashSet overrides the default toString() implementation, you can see the set contents with System.out.println(set);

    As arrays do not override the default toString() (that gives the class name and some sort of identity hash code), you are getting the fuzzy [Ljava.lang.String;@9b49e6

    Hope that helps

提交回复
热议问题