Why toString() method works differently between Array and ArrayList object in Java

末鹿安然 提交于 2019-12-17 22:41:58

问题


    String[] array = {"a","c","b"};
    ArrayList<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    System.out.println(array);
    System.out.println(list);

For list [a, b, c] is output while for array some address is output. When we want to output the array values, we can use Arrays.toString(array); which works just like list.

I just wonder why we can't call toString() directly on array to get the values. Isn't it more intuitive and convenient to do so? What results in the different treatments on Array and ArrayList?


回答1:


The main difference between an array and an arraylist is that an arraylist is a class that is written in Java and has its own implementation (including the decision to override toString) whereas arrays are part of the language specification itself. In particular, the JLS 10.7 states:

The members of an array type are all of the following:

  • The public final field length
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions.
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

In other words the language specification prevents the toString method of an array to be overriden and it therefore uses the default implementation defined in Object which prints the class name and hashcode.

Why this decision has been made is a question that should probably be asked to the designers of the language...




回答2:


I just wonder why we can't call toString() directly on array to get the values.

Actually toString method is called on the array object. But, since array type does not override toString method from Object class, so default implementation of toString is invoked, that returns the representation of the form that you see.

The representation is of the form: -

[typeOfArray@hashCode

In your case it's something like: -

[Ljava.lang.String;@3e25a5

Whereas, in case of ArrayList instances, the overriden toString method in ArrayList class is invoked.




回答3:


The short answer is because toString is defined in a few different places, with different behaviours.

The Arrays class defines toString as a static method, to be invoked like

Arrays.toString(arr_name);

But the Arrays class also inherits the non-static method toString from the Object class. So if called on an instance, it invokes Object.toString which returns a string representation of the object (eg: [Ljava.lang.Object;@4e44ac6a)

So Arrays.toString() and MyObject.toString() are calling different methods with the same name.

The ArrayList class inherits toString from the AbstractCollection class, where it is a non static method, so can be called on the object like:

MyArrayList.toString();

Because it's a string representation of a collection and not an object, the result is the values in a readable format like [one, two].




回答4:


Because when you print toString(), it will by default print className@HashCode.

So, when you print array then above will be printed.

But ArrayList is extened by AbstractCollection class and where the toString() method is overriden as below

 public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }

which prints the readable format of the ArrayList object.




回答5:


This is the toString method call for ArrayList. But for Arrays you cant find such.

 /**
 * Returns a string representation of this collection.  The string
 * representation consists of a list of the collection's elements in the
 * order they are returned by its iterator, enclosed in square brackets
 * (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
 * <tt>", "</tt> (comma and space).  Elements are converted to strings as
 * by {@link String#valueOf(Object)}.
 *
 * @return a string representation of this collection
 */
public String toString() {
    Iterator<E> it = iterator();
    if (! it.hasNext())
        return "[]";

    StringBuilder sb = new StringBuilder();
    sb.append('[');
    for (;;) {
        E e = it.next();
        sb.append(e == this ? "(this Collection)" : e);
        if (! it.hasNext())
            return sb.append(']').toString();
        sb.append(',').append(' ');
    }
}



回答6:


For array toString() method it prints the memory address. But in ArrayList this class overrides Object toString() method .

toString() implementation of ArrayList

   public String toString() {
    Iterator<E> i = iterator();
    if (! i.hasNext())
        return "[]";

    StringBuilder sb = new StringBuilder();
    sb.append('[');
    for (;;) {
        E e = i.next();
        sb.append(e == this ? "(this Collection)" : e);
        if (! i.hasNext())
        return sb.append(']').toString();
        sb.append(", ");
    }
}


来源:https://stackoverflow.com/questions/13780374/why-tostring-method-works-differently-between-array-and-arraylist-object-in-ja

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!